Python/Selenium incognito/private mode
Asked Answered
C

10

75

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?

Compost answered 24/12, 2014 at 1:6 Comment(3)
possible duplicate of Python - Start firefox with Selenium in private modeSpurlock
@Spurlock yeah, might be a duplicate, but I think I've managed to summarize the idea in a single answer and provide more python-specific options. Thanks.Liston
@Liston I agree that your answer here is more detailed and thus is the one that should be given priority. However, the upshot is that the other question to which you posted an answer is a duplicate of this one. (It is completely fine per SO customs to vote as duplicate the question with the answers that are of lesser quality, irrespective of which question was posted first.) Note that the regulars on Meta take a dim view of users who post answers to a question and its duplicate(s).Deneendenegation
L
107

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:

happy holidays!

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:

enter image description here

Liston answered 24/12, 2014 at 1:12 Comment(9)
Do you know how to in firefox?Compost
I understand what you mean in "Python - Start firefox with Selenium in private mode" but i am running multiple things at once and ingonito is needed as other wise the url is redirected based on what the previous is doing. ThanksCompost
It is important to enforce the actual incognito mode when testing. For example, Safari prohibits writing to local storage when in Private mode. Thus, it is important to run tests in a manner that would detect bugs relating to this limitation.Geophilous
Copy/paste of this code doesnt work for me (latest Linux/Mint distro). Everything works except that it's not started in incognito mode. Any idea?Brookhouse
The last option definitely made me throw away chrome and keep Firefox for my main development right now. See #49895323Brookhouse
Note that incognito seems to be much faster when testing heavy websites like Amazon. I assume that is since there is less connection overhead for fetching personalized ads, etc. plus a much faster loading and teardown.Ashok
This resolve for me, only changed it for my use with php $chrome_options->addArguments(["--incognito"]);Considering
It was working earlier with older version of chrome but with newer version it's no longer working. failed on chrome v106Gropius
TypeError: __init__() got an unexpected keyword argument 'firefox_profile'Fraley
H
22

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')
Hatshepsut answered 14/6, 2019 at 6:50 Comment(0)
R
5

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);
Rainer answered 23/4, 2018 at 17:27 Comment(0)
S
4

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

Sixfooter answered 15/3, 2018 at 15:22 Comment(0)
I
3

For firefox : (Python) ==>

from selenium import webdriver    
firefox_options = webdriver.FirefoxOptions()
firefox_options.add_argument("--private")
browser = webdriver.Firefox(firefox_options=firefox_options)
Independent answered 25/6, 2018 at 12:43 Comment(4)
Close: options.add_argument("-private") developer.mozilla.org/en-US/docs/Mozilla/Command_Line_OptionsSmallscale
it still works. However got this : DeprecationWarning: use options instead of firefox_options browser = webdriver.Firefox(firefox_options=firefox_options)Globuliferous
("--private"), ("-private ") Both Are The Same For Linux . Not sure about Windows! You May Also Use ("-private-window") Instead Of ("-private"). @SmallscaleIndependent
@Globuliferous , If options works instead of firefox_options, Then Use It !Independent
H
2
//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");
Historical answered 27/3, 2021 at 1:1 Comment(0)
F
1

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".

Frink answered 26/7, 2019 at 15:6 Comment(0)
M
1

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)
Milreis answered 25/5, 2020 at 2:32 Comment(0)
S
0

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)
Swink answered 23/5, 2017 at 21:4 Comment(0)
R
0

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)
Rosaceous answered 21/3, 2022 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.