1

I am trying to test Selenium in incognito mode. I've looked up online about how to make it in Python but couldn't really find a new version of the procedure. From what I could find, I made following script:

from selenium.webdriver import Chrome
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  
options = Options()
options.add_argument("−−incognito")
c = DesiredCapabilities.CHROME.copy()

s = Service('C:\Program Files (x86)\chromedriver.exe')
driver = Chrome(service=s, options=options, desired_capabilities=c)

But this doesn't make the browser to launch in incognito mode. What is wrong here? What could I add to make it work?

Donnerhorn
  • 59
  • 3
  • 9
  • Does this answer your question? [Python/Selenium incognito/private mode](https://stackoverflow.com/questions/27630190/python-selenium-incognito-private-mode) – Maximilian Burszley Mar 25 '22 at 13:09

2 Answers2

1

You don't need to pass the DesiredCapabilities object unless you are using the Remote WebDriver.


Solution

As you are using Chrome() effectively your code block will be:

from selenium.webdriver import Chrome
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("−−incognito")
s = Service('C:\Program Files (x86)\chromedriver.exe')
driver = Chrome(service=s, options=options)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I tried this one, and had it close to similar before as well. Sadly it doesn't make the driver start in incognito mode for me. – Donnerhorn Mar 25 '22 at 14:00
  • Just a question. Why incognito? As much as I used selenium(though I didn't use much) it opens a complete different window which has no history/cookies/accounts and neither it does save them anywhere. It's kinda like incognito itself so why to open it in incognito window? – I-am-developer-9 Mar 25 '22 at 15:54
  • If you have scenarios where you need to test multiple user logins for concurrency or whatever, you need to use incognito to "be safe". Given that cookies and other Windows behavior that happens where if you're logged into browser instance A and open a new instance B you're still logged in as the same user as instance A. Logging out one logs out both. – Earl G Elliott III Jan 04 '23 at 18:27
0

Your initial code is pretty much ok, the only change you need is to lose the dashes for the incognito option.

options.add_argument("incognito")
knipp
  • 196
  • 4
  • 12