'Selenium Chrome Webdriver in Incognito Mode

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?



Solution 1:[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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 undetected Selenium