5 Best Ways to Open a Browser Window in Incognito/Private Mode Using Python Selenium WebDriver

Rate this post

πŸ’‘ Problem Formulation: When automating web browsers using Selenium WebDriver in Python, there are times when a user needs to test features or scrape data without retaining any user data or cookies. This requires opening a browser window in incognito or private mode. The goal here is to provide code snippets that launch a browser in private mode, starting from a Python script and resulting in an incognito browser session.

Method 1: Using ChromeOptions to launch Chrome in Incognito Mode

This method involves configuring a ChromeOptions object to add the incognito argument before initiating the WebDriver. It’s a straightforward approach to ensure that the Chrome browser opens in incognito mode, thus not saving any browsing history or cookies.

Here’s an example:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--incognito")
chrome_driver_path = '/path/to/chromedriver'
driver = webdriver.Chrome(options=options, executable_path=chrome_driver_path)
driver.get('https://www.google.com')

The above code initiates a Google Chrome browser in incognito mode and opens Google’s homepage.

In this snippet, Options is imported from selenium.webdriver.chrome.options, and an instance is created. The --incognito argument is added to the options, which is then passed to the webdriver.Chrome() constructor along with the path to your chromedriver executable. The browser will open in incognito mode.

Method 2: Firefox WebDriver Using FirefoxOptions to Open Private Window

To open Mozilla Firefox in private browsing mode, you can set the relevant FirefoxOptions before initiating the WebDriver. This works similarly to the ChromeOptions method but with Firefox-specific settings.

Here’s an example:

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

options = Options()
options.add_argument("-private")
firefox_driver_path = '/path/to/geckodriver'
driver = webdriver.Firefox(options=options, executable_path=firefox_driver_path)
driver.get('https://www.google.com')

The output is Mozilla Firefox launching in private mode and opening the specified URL.

The code sets up an instance of Options from selenium.webdriver.firefox.options and then adds the -private argument for private mode. The configured options are provided when creating the Firefox WebDriver, which causes Firefox to open in private mode.

Method 3: Using DesiredCapabilities to Launch Browser in Incognito/Private Mode

DesiredCapabilities can be used for more detailed customization of the WebDriver’s behavior. These capabilities allow users to define specific properties that the browser session should have.

Here’s an example:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

capabilities = DesiredCapabilities.CHROME.copy()
capabilities['goog:chromeOptions'] = {'args': ['--incognito']}
chrome_driver_path = '/path/to/chromedriver'
driver = webdriver.Chrome(desired_capabilities=capabilities, executable_path=chrome_driver_path)
driver.get('https://www.google.com')

This results in Google Chrome being launched with the incognito flag applied, therefore in incognito mode.

This code shows how DesiredCapabilities for Chrome are copied and modified to include Chrome options, specifically, the incognito argument. The updated capabilities are then used to create the Chrome WebDriver.

Method 4: Setting Safari to Private Browsing Mode

On macOS, Safari can also be used with Selenium. It’s a bit more complex to set private mode in Safari programmatically, but it’s still possible with a combination of Selenium WebDriver settings and AppleScript.

Here’s an example:

from selenium import webdriver
import os

safari_driver_path = '/path/to/safaridriver'
driver = webdriver.Safari(executable_path=safari_driver_path)
driver.get('https://www.google.com')
os.system("osascript -e 'tell application \"Safari\" to activate' -e 'tell application \"System Events\" to keystroke \"n\" using {command down, shift down}'")

A new Safari window will be opened in private browsing mode.

In this approach, after initializing the Safari WebDriver, an AppleScript is executed through the os.system call to open a new private window. This relies on keyboard shortcuts and interface scripting, so it might not be as reliable as other methods.

Bonus One-Liner Method 5: Simplified Chrome Incognito with WebDriver

For those preferring a one-liner initialization, here’s a simplified version to launch Chrome in incognito mode using Selenium WebDriver.

Here’s an example:

webdriver.Chrome(executable_path='/path/to/chromedriver', options=webdriver.ChromeOptions().add_argument("--incognito")).get('https://www.google.com')

The browser opens the Google homepage in incognito mode directly.

This compact code does everything in one line: it imports Chrome WebDriver, sets up the options with the incognito argument, and opens a URL. While concise, it’s best used when only a single WebDriver operation is needed.

Summary/Discussion

    Method 1: ChromeOptions with Incognito. Strengths: Simple and straightforward. Weaknesses: Specific to Chrome. Method 2: FirefoxOptions for Private Browsing. Strengths: Straightforward for Firefox users. Weaknesses: Firefox-specific; not applicable to other browsers. Method 3: DesiredCapabilities Customization. Strengths: Highly customizable, works with multiple browsers. Weaknesses: More complex, can be overkill for simple needs. Method 4: Safari and AppleScript. Strengths: Enables Safari private browsing on macOS. Weaknesses: Relies on system-specific scripting, less reliable. One-Liner Method 5: Simplified Chrome Incognito. Strengths: Extremely concise code for simple tasks. Weaknesses: Offers less control and flexibility than full implementations.