Python/Selenium incognito/private mode

匿名 (未验证) 提交于 2019-12-03 02:54:01

问题:

I can not seem to find any documentation on how to make Selenium open the browser in incognito mode.

Do I have to setup a custom profile in the browser or?

回答1:

First of all, since selenium by default starts up a browser with a clean, brand-new profile, you are actually already browsing privately. Referring to:


But you can strictly enforce/turn on incognito/private mode anyway.

For chrome pass --incognito command-line argument:

--incognito Causes the browser to launch directly in incognito mode.

from selenium import webdriver  chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--incognito")  driver = webdriver.Chrome(chrome_options=chrome_options) driver.get('https://google.com') 

FYI, here is what it would open up:

For firefox, set browser.privatebrowsing.autostart to True:

from selenium import webdriver  firefox_profile = webdriver.FirefoxProfile() firefox_profile.set_preference("browser.privatebrowsing.autostart", True)  driver = webdriver.Firefox(firefox_profile=firefox_profile) 

FYI, this corresponds to the following checkbox in settings:



回答2:

PowerShell

try{     # Import the Selenium DLLs     Add-Type -Path "$Seleniumlib\Selenium.WebDriverBackedSelenium.dll"     Add-Type -Path "$Seleniumlib\WebDriver.dll"     Add-Type -Path "$Seleniumlib\WebDriver.Support.dll" } catch [Exception]{     Write-Host ("Error: {0}" -f $_.Exception.Message)     exit 1 }  $options = New-Object OpenQA.Selenium.Chrome.ChromeOptions $options.AddArgument("--incognito") $driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($options) 


回答3:

There is a really simple way to make a window open in incognito mode:

from selenium.webdriver.chrome.options import Options  chrome_options = Options() # incognito window chrome_options.add_argument("--incognito") 

You can also use this library for maximizing the window and more, see the documentation: https://seleniumhq.github.io/selenium/docs/api/rb/Selenium/WebDriver/Chrome/Options.html



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!