I am currently automating a suite of tests for a web application where I need to verify that user sessions are not stored between test runs. I’m using the Selenium WebDriver with Python and want to know how to programmatically trigger the Chrome browser to open in Incognito mode. Is there a specific argument or capability I need to pass into the ChromeOptions to achieve this?
3 answers
To launch Chrome in Incognito mode, you need to use the ChromeOptions class provided by Selenium. You instantiate the options and then use the add_argument() method to pass the --incognito flag. In Python, your code would look like this: options = webdriver.ChromeOptions() followed by options.add_argument("--incognito"). Finally, you pass these options into your WebDriver initialization. This is a standard practice in automated testing to ensure a clean state, as it prevents the browser from reading or writing cookies and cache from previous sessions, which is crucial for verifying login flows and tracking pixels.
Does opening the browser in Incognito mode also disable all your Chrome extensions by default during the Selenium session, or do you have to manually disable those as well to ensure a clean test environment?
You can also achieve this in Java by using options.addArguments("--incognito"); before passing the options object to the ChromeDriver constructor. It works identically across languages.
I agree with Susan; the flag remains consistent whether you are using Python, Java, or C#. It’s one of the most reliable Chrome switches for Selenium automation enthusiasts.
That's a perceptive question, Justin. When Selenium launches Chrome, it typically uses a fresh, temporary profile which already excludes your personal extensions. However, using the --incognito flag further ensures that no local storage or session data is persisted. If you specifically need to test with an extension in Incognito, you actually have to jump through a few more hoops in the manifest settings. For most Software Development testing, simply using the incognito flag is sufficient to isolate the browser environment and mimic a first-time user experience perfectly.