72

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?

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
BubblewrapBeast
  • 1,507
  • 2
  • 15
  • 19
  • possible duplicate of [Python - Start firefox with Selenium in private mode](http://stackoverflow.com/questions/27425116/python-start-firefox-with-selenium-in-private-mode) – AlexMeng Dec 24 '14 at 01:16
  • @AlexMeng 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. – alecxe Dec 24 '14 at 01:30
  • 1
    @alecxe 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). – Louis Dec 24 '14 at 17:32

10 Answers10

104

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

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Do you know how to in firefox? – BubblewrapBeast Dec 24 '14 at 01:14
  • 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. Thanks – BubblewrapBeast Dec 24 '14 at 01:26
  • 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. – Zack May 20 '15 at 12:39
  • 2
    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? – Olivier Pons Apr 18 '18 at 06:49
  • The last option definitely made me throw away chrome and keep Firefox for my main development right now. See https://stackoverflow.com/questions/49894823/avoid-accessing-shadow-element-in-selenium-chrome – Olivier Pons Apr 18 '18 at 08:51
  • 1
    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. – Pithikos Feb 20 '19 at 12:10
  • This resolve for me, only changed it for my use with php ```$chrome_options->addArguments(["--incognito"]);``` – Paulo Matos Apr 19 '22 at 12:06
  • It was working earlier with older version of chrome but with newer version it's no longer working. failed on chrome v106 – Vikas Thange Nov 03 '22 at 14:04
  • `TypeError: __init__() got an unexpected keyword argument 'firefox_profile'` – axolotl Jun 12 '23 at 20:32
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')
4b0
  • 21,981
  • 30
  • 95
  • 142
Swaraj S
  • 221
  • 2
  • 2
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);
Kireeti Annamaraj
  • 1,037
  • 1
  • 8
  • 12
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

Gavriel Cohen
  • 4,355
  • 34
  • 39
3

For firefox : (Python) ==>

from selenium import webdriver    
firefox_options = webdriver.FirefoxOptions()
firefox_options.add_argument("--private")
browser = webdriver.Firefox(firefox_options=firefox_options)
A_Asaker
  • 63
  • 6
  • Close: `options.add_argument("-private")` https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options – wp78de Jul 14 '18 at 00:22
  • it still works. However got this : DeprecationWarning: use options instead of firefox_options browser = webdriver.Firefox(firefox_options=firefox_options) – luney Jun 04 '19 at 03:31
  • `("--private")`, `("-private ")` Both Are The Same For Linux . Not sure about Windows! You May Also Use `("-private-window")` Instead Of `("-private")`. @wp78de – A_Asaker Jul 06 '19 at 15:45
  • @luney , If `options` works instead of `firefox_options`, Then Use It ! – A_Asaker Jul 06 '19 at 15:48
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");
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".

chawlajc
  • 9
  • 1
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)
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)
Adarsha
  • 2,267
  • 22
  • 29
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)
Hakun
  • 357
  • 3
  • 10