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?
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?
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:
$chrome_options->addArguments(["--incognito"]);
–
Considering TypeError: __init__() got an unexpected keyword argument 'firefox_profile'
–
Fraley Note: chrome_options is now deprecated. We can use 'options' instead of chrome_options
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
driver = webdriver.Chrome(options=options)
driver.get('https://google.com')
I have initiated both Chrome and Firefox in incognito/Private mode using ChromeOptions and FirefoxOptions successfully using the code snippets in Java as below:
//For Firefox
FirefoxOptions options = new FirefoxOptions();
options.addArguments("-private");
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("moz:firefoxOptions",options);
//For Chrome
ChromeOptions options = new ChromeOptions();
options.addArguments("-incognito");
caps.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
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
For firefox : (Python) ==>
from selenium import webdriver
firefox_options = webdriver.FirefoxOptions()
firefox_options.add_argument("--private")
browser = webdriver.Firefox(firefox_options=firefox_options)
options.add_argument("-private")
developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options –
Smallscale ("--private")
, ("-private ")
Both Are The Same For Linux . Not sure about Windows! You May Also Use ("-private-window")
Instead Of ("-private")
. @Smallscale –
Independent options
works instead of firefox_options
, Then Use It ! –
Independent //We need to add argument "--incogneto" in ChromeOptions object and pass this ChromeOptions instance to the web driver initialization.
ChromeOptions options = new ChromeOptions()
options.addArgument("start-maximized");
options.addArgument("--incognito");
ChromeDriver driver = new ChromeDriver(options);
driver.get("https://investopedia.com");
In Chrome Browser You Can Do This Using Python As Follows
As you can see when you uses chrome, you have the option of incognito mode in the options menu part of the chrome browser. So when you are using selenium, you can alter the things of options using
chrome_options = webdriver.ChromeOptions()
So, the code is:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(executable_path="<path of chrome_driver.exe file>",options=chrome_options)
So the only thing you have to do is to give "webdriver.Chrome" this given value to its another parameter i.e. "options".
For python with opera
from selenium import webdriver
options = webdriver.opera.webdriver.Options()
options.add_argument("private")
driver = webdriver.Opera(executable_path="operadriver",options=options)
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)
Some options have been deprecated so for Firefox it worked out for me like this:
from selenium.webdriver.firefox.options import Options
from selenium import webdriver
firefox_options = Options()
firefox_options.add_argument("-private")
driver = webdriver.Firefox(options=firefox_options)
© 2022 - 2024 — McMap. All rights reserved.