How to open browser window in incognito/private mode using selenium webdriver?
Last Updated :
27 Aug, 2024
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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://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.
Similar Reads
How to Open a Browser in Headless Mode in Selenium using Java? Headless testing has become a significant trend in modern web automation, offering numerous advantages for developers and testers. In Selenium, running tests in headless mode allows browsers to operate without the graphical user interface (GUI). This article delves into the concept of headless brows
6 min read
How to run Selenium Running Test on Chrome using WebDriver Selenium is a popular open-source tool for automating web browser interactions. It works with different web browsers like Chrome, Firefox, and more, and different programming languages like Python or Java to write tests. What is a Selenium ChromeDriver?ChromeDriver is a separate executable that Sele
3 min read
How to handle windows file upload using Selenium WebDriver? Handling file uploads in Selenium WebDriver is a common task in automation testing. In most web applications, uploading files is a critical feature that requires testing to ensure seamless functionality. Selenium WebDriver provides an easy and efficient way to automate file uploads. Unlike typical u
3 min read
How to set browser width and height in Selenium WebDriver? Using Selenium WebDriver, we can set the browser width and height. Various methods are available to set browser width and height. When we run any test, the browser will open in default size. So, if we need to change the size of the browser, we can do it using selenium WebDriver keywords. A few of th
2 min read
How to hide Firefox window (Selenium WebDriver)? In situations where you might wish to execute tests without a visible browser window, while automating web tests with Selenium WebDriver; it is possible for one to run headless browsers for example which will help save resources and speed up the execution of these tests. This article will cover how
3 min read