Incognito Mode

Introduction

Incognito Mode (also called Private Browsing) is a browser feature that allows Selenium to launch a browser session without saving browsing history, cookies, cache, site data, or form information after the browser is closed.

Running Selenium tests in Incognito Mode helps create a clean browser session every time the test starts. This prevents previously stored cookies, cached files, or login sessions from affecting test execution, making automated tests more reliable and consistent.

Incognito Mode is especially useful for testing login functionality, user authentication, session management, and applications where browser data should not persist between test runs.

In this tutorial, you will learn what Incognito Mode is, why it is used, its syntax, practical examples, real-world applications, common mistakes, best practices, and frequently asked interview questions.


What is Incognito Mode?

Incognito Mode launches the browser in a private browsing session.

When Selenium starts Chrome in Incognito Mode:

  • Browsing history is not saved.

  • Cookies are deleted after the browser closes.

  • Cache is not retained.

  • Form data is not stored.

  • Login sessions are cleared after closing the browser.

This provides a fresh browser environment for every test execution.


Why Do We Use Incognito Mode?

Incognito Mode is commonly used to:

  • Execute tests in a clean browser session.

  • Prevent cached data from affecting tests.

  • Test login and logout functionality.

  • Verify session management.

  • Avoid browser history and cookie persistence.

  • Improve automation reliability.


Syntax

Create Chrome Options

options = webdriver.ChromeOptions()

Enable Incognito Mode

options.add_argument("--incognito")

Launch Chrome in Incognito Mode

driver = webdriver.Chrome(options=options)

Practical Example

The following example demonstrates how to launch Google Chrome in Incognito Mode.

The automation script creates Chrome Options, enables Incognito Mode, launches Chrome, opens the Selenium practice website, and verifies the page title.

from selenium import webdriver


# Topic: 8. Browser Configuration - Incognito Mode
# Practice site: https://the-internet.herokuapp.com/
#
# Incognito mode starts a private browsing session that does not persist
# cookies or history after the browser is closed.


def test_incognito_mode():
    options = webdriver.ChromeOptions()
    options.add_argument("--incognito")

    driver = webdriver.Chrome(options=options)

    try:
        driver.get("https://the-internet.herokuapp.com/")

        assert driver.title == "The Internet"

    finally:
        driver.quit()

Output

Chrome launched successfully in Incognito Mode.

Private browsing session started.

Website opened successfully.

Page Title:
The Internet

Assertion Passed.

Test Executed Successfully.

Understanding the Code

Import WebDriver

from selenium import webdriver

Imports the Selenium WebDriver module.


Create Chrome Options

options = webdriver.ChromeOptions()

Creates a Chrome Options object to configure browser startup.


Enable Incognito Mode

options.add_argument("--incognito")

Configures Chrome to launch in Incognito Mode.


Launch Chrome

driver = webdriver.Chrome(options=options)

Starts Chrome using the configured browser options.


Open the Website

driver.get("https://the-internet.herokuapp.com/")

Navigates to the Selenium practice website.


Verify the Page Title

assert driver.title == "The Internet"

Confirms that the webpage loaded successfully.


Close the Browser

driver.quit()

Closes all browser windows and ends the WebDriver session.


Browser Execution Flow

Create Chrome Options
        │
        ▼
Enable Incognito Mode
        │
        ▼
Launch Chrome
        │
        ▼
Private Browsing Session Starts
        │
        ▼
Open Website
        │
        ▼
Verify Page Title
        │
        ▼
Assertions Pass
        │
        ▼
Close Browser
        │
        ▼
Cookies and History Removed

Advantages of Incognito Mode

  • Starts every test with a clean browser session.

  • Prevents cached data from affecting automation.

  • Removes cookies after the browser closes.

  • Improves test reliability.

  • Useful for authentication and session testing.

  • Eliminates dependency on previous browser activity.


Automation Testing Example

Suppose you’re testing the login functionality of an online banking application.

If previous login cookies remain in the browser, Selenium may automatically log in without displaying the login page.

Launching the browser in Incognito Mode ensures that:

  • No previous session exists.

  • The login page always appears.

  • Authentication is tested from a fresh state.


Real-World Example

Consider an e-commerce website where users can browse products without logging in.

Your automation framework runs multiple user scenarios throughout the day.

By using Incognito Mode, every test starts with:

  • No stored cookies.

  • No shopping cart history.

  • No cached pages.

  • No previous login session.

This guarantees consistent and repeatable test results.


Common Mistakes Beginners Make

Forgetting to Pass Chrome Options

Creating Chrome Options alone does not enable Incognito Mode.

Incorrect

options = webdriver.ChromeOptions()

options.add_argument("--incognito")

driver = webdriver.Chrome()

Correct

driver = webdriver.Chrome(options=options)

Assuming Incognito Disables All Tracking

Incognito Mode only prevents data from being stored locally after the session ends.

Websites, network administrators, and internet service providers can still monitor network activity.


Adding Browser Arguments After Launching Chrome

Browser arguments must be configured before creating the WebDriver instance.


Best Practices

  • Use Incognito Mode when testing login and authentication features.

  • Combine Incognito Mode with a fresh browser session for reliable testing.

  • Configure browser options before launching Chrome.

  • Do not rely solely on Incognito Mode for complete test isolation if your framework requires additional cleanup.

  • Use the latest Selenium and browser versions.

  • Always close the browser using driver.quit().


Conclusion

Incognito Mode allows Selenium to launch Chrome in a private browsing session where cookies, browsing history, cache, and other browsing data are not retained after the browser is closed.

It helps automation engineers execute tests in a clean environment, improving reliability and reducing failures caused by leftover browser data. Incognito Mode is commonly used for authentication testing, session management, and regression testing.


Frequently Asked Questions (FAQs)

What is Incognito Mode in Selenium?

Incognito Mode launches the browser in a private browsing session that does not retain browsing history, cookies, or cache after the browser is closed.


How do I enable Incognito Mode in Chrome?

options = webdriver.ChromeOptions()

options.add_argument("--incognito")

How do I launch Chrome in Incognito Mode?

driver = webdriver.Chrome(options=options)

Why is Incognito Mode useful in automation testing?

It ensures every test starts with a clean browser session, preventing cached data, cookies, or previous login sessions from affecting test execution.


Does Incognito Mode delete cookies?

Yes.

Cookies created during the session are automatically removed when the Incognito browser window is closed.


Key Takeaways

  • Incognito Mode launches Chrome in a private browsing session.

  • Enable Incognito Mode using options.add_argument("--incognito").

  • Launch Chrome with webdriver.Chrome(options=options).

  • Incognito Mode prevents browsing history, cookies, and cache from being retained after the session ends.

  • It is useful for login testing, session management, and regression testing.

  • Every automation run starts with a clean browser environment.

  • Configure browser options before creating the WebDriver instance.

  • Incognito Mode is a commonly used Selenium browser configuration feature and an important interview topic.