How to open browser window in incognito/private mode using selenium webdriver?
Last Updated :
23 Jul, 2025
Selenium WebDriver is one of the most used tools for browser automation for the purpose of testing web applications. This makes it possible to automate a browser through the use of a simple API via different programming languages such as Java, C#, Python, and so on.
While testing, sometimes you may wish to open the browser window in incognito or private mode to ensure that no data/cache from previous sessions influences the current session. Popular browsers such as Google Chrome and Firefox also have the option to open a website in incognito/private mode. In this article, let us understand how to open Chrome as well as Firefox browsers in Incognito/Private mode using Java bindings for Selenium.
Example of Opening Chrome in Incognito Mode
To launch Chrome in incognito mode using Selenium WebDriver, you need to add the --incognito
argument to the ChromeOptions before initializing the WebDriver.
Dependency for the same:
Pom.XML
XML
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>selenium-incognito-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- Selenium Java dependency -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.11.0</version>
</dependency>
<!-- WebDriverManager dependency -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Here’s how you can do it:
ChromeDriverTest1.java
Java
package com.example.tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import io.github.bonigarcia.wdm.WebDriverManager;
public class ChromeDriverTest1 {
public static void main(String[] args) {
try {
// Set up ChromeDriver using WebDriverManager
WebDriverManager.chromedriver().setup();
// Initialize ChromeOptions to configure browser settings
ChromeOptions options = new ChromeOptions();
// Add the argument for incognito mode
options.addArguments("--incognito");
// Initialize ChromeDriver instance with options
WebDriver driver = new ChromeDriver(options);
// Maximize the browser window
driver.manage().window().maximize();
// Open a website to verify the incognito mode
driver.get("https://www.google.com/");
// Pause the execution for 5 seconds to see the process
Thread.sleep(5000);
// Add any additional test steps here
// Close the browser
driver.quit();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Output:
When you run the above scripts, the respective browser (Chrome or Firefox) will launch in incognito or private mode. The browser window will not save any browsing history, cookies, or other session data. This can be confirmed by checking the browser interface, which should indicate that it is in private/incognito mode (e.g., "Incognito" label in Chrome or "Private Browsing" label in Firefox).
incognito-by-seleniumConclusion
Opening a browser window in Incognito mode using Java Selenium WebDriver is a simple yet powerful technique for ensuring a clean testing environment. By using this method, you can effectively test features that depend on session data, cookies, or user-specific settings. Implementing Incognito mode in your automated tests not only enhances test reliability but also provides a more comprehensive approach to browser automation with Selenium WebDriver.