askvity

How to Open Firefox Private Window in Selenium?

Published in Selenium Firefox Private Mode 3 mins read

Opening Firefox in a private browsing window using Selenium involves setting specific arguments or preferences before launching the browser instance. The core concept, as suggested by the provided reference, is to add a command-line argument like --private or -private to the browser options.

Launching Firefox in Private Mode

To launch Firefox in private mode with Selenium, you need to configure the browser options before creating the WebDriver instance. This is typically done by adding an argument that tells Firefox to start a private browsing session.

Based on the reference provided, a key piece of information is the use of the -private command:

  • "So i can provide hyphen private. So this command will help me to launch the browser in a private mode."

This indicates that passing -private (or --private depending on the exact implementation/version) as an argument when launching Firefox is the method. In Selenium, you achieve this by using the FirefoxOptions class.

Steps to Open Firefox Private Window

Here's a general approach using Python as an example:

  1. Import necessary classes: You'll need webdriver from selenium and FirefoxOptions from selenium.webdriver.firefox.options.
  2. Create a FirefoxOptions object: This object allows you to set various configurations for the Firefox browser.
  3. Add the private argument: Use the add_argument() method of the FirefoxOptions object to include the command-line argument for private mode.
  4. Instantiate the WebDriver: Pass the configured FirefoxOptions object when creating the Firefox WebDriver instance.

Practical Example (Python)

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

# 1. Create a FirefoxOptions object
options = Options()

# 2. Add the argument for private mode
# The exact argument might be --private or -private depending on Firefox/Selenium version
# --private is more commonly used now, but -private was mentioned in the reference.
# It's best to try --private first, or consult Selenium/GeckoDriver docs if needed.
options.add_argument("--private")

# 3. Instantiate the WebDriver with the options
# Ensure your geckodriver is in your system's PATH or specify its location
try:
    driver = webdriver.Firefox(options=options)
    print("Firefox launched in private browsing mode!")

    # Example: Navigate to a website
    driver.get("https://www.example.com")

    # Your automation steps here...

    # Close the browser
    # driver.quit()

except Exception as e:
    print(f"An error occurred: {e}")

# Remember to call driver.quit() when you are done

Explanation:

  • We import Options to configure Firefox.
  • We create an Options object.
  • options.add_argument("--private") is the crucial line. It tells Firefox to start in private browsing mode. While the reference mentioned -private, --private is the standard flag for enabling private browsing mode from the command line in recent Firefox versions, and Selenium/GeckoDriver typically pass these arguments directly to the browser executable. It's recommended to use --private.
  • webdriver.Firefox(options=options) launches the Firefox browser with the specified configurations.

By following these steps, you can successfully launch a Firefox private browsing window using Selenium, allowing you to perform automation tasks without leaving persistent browsing history, cookies, or cache from that session.

Related Articles