List of Selenium Chrome Options [ChromeOptions] – WebDriver

In Selenium, Chrome options allow you to customize the behavior of the Chrome browser when automating tests. You can set different options such as running Chrome in headless mode, setting window sizes, and configuring the Chrome driver with various preferences and flags.

Setting Chrome Options in Selenium

To use Chrome options in Selenium, you will typically create an instance of ChromeOptions and add desired flags or preferences to it. Here’s a basic example of how to set Chrome options in Selenium:

Example in Python (Selenium with ChromeDriver):

from selenium import webdriver

# Create an instance of ChromeOptions
chrome_options = webdriver.ChromeOptions()

# Add options to the instance (examples below)
chrome_options.add_argument("--headless") # Run Chrome in headless mode
chrome_options.add_argument("--no-sandbox") # Disable sandbox for running in CI
chrome_options.add_argument("--disable-gpu") # Disable GPU hardware acceleration
chrome_options.add_argument("--start-maximized") # Open Chrome maximized
chrome_options.add_argument("--incognito") # Open in incognito mode
chrome_options.add_argument("--disable-extensions") # Disable extensions
chrome_options.add_argument("--window-size=1920x1080") # Set window size

# Launch Chrome with the options
driver = webdriver.Chrome(options=chrome_options)

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

# Close the browser
driver.quit()

Commonly Used Chrome Options

Here is a list of commonly used Chrome options:

1. Headless Mode

  • Purpose: Run Chrome without a GUI.
chrome_options.add_argument("--headless")

2. Incognito Mode

  • Purpose: Open Chrome in incognito (private browsing) mode.
chrome_options.add_argument("--incognito")

3. Disable GPU

  • Purpose: Disable GPU hardware acceleration.
chrome_options.add_argument("--disable-gpu")

4. No-Sandbox Mode

  • Purpose: Disable the sandbox security feature (useful in CI environments).
chrome_options.add_argument("--no-sandbox")

5. Start Maximized

  • Purpose: Open Chrome in a maximized window.
chrome_options.add_argument("--start-maximized")

6. Disable Extensions

  • Purpose: Disable Chrome extensions.
chrome_options.add_argument("--disable-extensions")

7. Set Window Size

  • Purpose: Set a specific window size for the Chrome browser.
chrome_options.add_argument("--window-size=1920x1080")

8. Disable Notifications

  • Purpose: Disable Chrome’s notification prompts.
chrome_options.add_argument("--disable-notifications")

9. Disable InfoBars

  • Purpose: Disable the info bars that appear at the top of Chrome.
chrome_options.add_argument("--disable-infobars")

10. Disable Logging

  • Purpose: Disable logging of Chrome’s browser console output.
chrome_options.add_argument("--disable-logging")

11. Use a Specific User Data Directory

  • Purpose: Use a specific directory for user data (such as bookmarks, extensions, etc.).
chrome_options.add_argument("--user-data-dir=/path/to/your/custom/profile")

12. Proxy Settings

  • Purpose: Configure a proxy for Chrome.
chrome_options.add_argument("--proxy-server=http://proxy-server:port")

13. Disable Image Loading

  • Purpose: Disable images from loading to speed up testing.
prefs = {"profile.managed_default_content_settings.images": 2}
chrome_options.add_experimental_option("prefs", prefs)

14. Enable Remote Debugging

  • Purpose: Enable remote debugging of Chrome (e.g., using Chrome DevTools Protocol).
chrome_options.add_argument("--remote-debugging-port=9222")

15. Add Extensions

  • Purpose: Add Chrome extensions during browser startup.
chrome_options.add_extension('/path/to/extension.crx')

16. Set Preferences (Advanced Settings)

  • Purpose: Set custom preferences for the Chrome browser (e.g., disable password saving).
prefs = {"credentials_enable_service": False, "profile.password_manager_enabled": False}
chrome_options.add_experimental_option("prefs", prefs)

Newly Available Chrome Options in Recent Versions

As Chrome updates frequently, new flags and options are introduced in each version. Below are some newly available options in recent Chrome versions (as of the latest updates):

1. Headless Debugging Mode (Chromium 91 and newer)

  • Purpose: In headless mode, Chrome can now provide more information for debugging.
chrome_options.add_argument("--headless=new")

2. WebSecurity Disable (Chromium 91 and newer)

  • Purpose: Disable web security features for testing cross-origin requests and other web security limitations.
chrome_options.add_argument("--disable-web-security")

3. Font Rendering

  • Purpose: For better font rendering in headless mode.
chrome_options.add_argument("--enable-font-antialiasing")

4. Disable Site Isolation (Chromium 92 and newer)

  • Purpose: Disable site isolation to reduce memory usage and increase performance.
chrome_options.add_argument("--disable-site-isolation-trials")

5. Experimental Features

  • Purpose: Enable experimental features that are not yet available to all users.
chrome_options.add_argument("--enable-experimental-web-platform-features")

6. Enable Automation (For detecting automation script running)

  • Purpose: To enable Chrome’s behavior of detecting automated scripts for better handling.
chrome_options.add_argument("--enable-automation")

7. Force Device Emulation (Mobile Emulation)

  • Purpose: Force Chrome to emulate a particular mobile device (like iPhone, Pixel, etc.).
mobile_emulation = {"deviceName": "Pixel 5"}
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)

Full Example of Chrome Options Usage (Python Selenium)

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()

# Basic options
chrome_options.add_argument("--headless") # Headless mode
chrome_options.add_argument("--incognito") # Incognito mode
chrome_options.add_argument("--start-maximized") # Maximize window
chrome_options.add_argument("--disable-gpu") # Disable GPU hardware acceleration
chrome_options.add_argument("--no-sandbox") # Disable sandbox (CI environments)

# Advanced preferences
prefs = {"profile.managed_default_content_settings.images": 2} # Disable image loading
chrome_options.add_experimental_option("prefs", prefs)

# Add extensions
chrome_options.add_extension('/path/to/extension.crx')

# Launch the browser with options
driver = webdriver.Chrome(options=chrome_options)

# Go to a URL
driver.get("https://www.example.com")

# Perform actions...

# Quit the driver
driver.quit()

Summary of Key Chrome Options:

  • --headless: Run in headless mode (without GUI).
  • --incognito: Start Chrome in incognito mode.
  • --start-maximized: Open the window maximized.
  • --disable-gpu: Disable GPU hardware acceleration.
  • --window-size: Set the browser window size.
  • --disable-extensions: Disable browser extensions.
  • --proxy-server: Set a proxy server.
  • --user-data-dir: Specify a custom user data directory.
  • --remote-debugging-port: Enable remote debugging.
  • --disable-notifications: Disable notifications.

In addition to the common Chrome options I mentioned earlier, you can configure a variety of other Chrome settings directly from chrome_options by using experimental options or specific flags. These configurations are especially useful when dealing with certain advanced scenarios like modifying Chrome’s internal preferences, enabling/disabling specific features, or interacting with Chrome’s experimental settings.

Here are some additional Chrome options you can configure using the ChromeOptions class in Selenium:

1. Set Proxy Settings

You can set the proxy server for Chrome to route traffic through a specified server.

chrome_options.add_argument("--proxy-server=http://proxy-server:port")

Alternatively, use a preference:

prefs = {"proxy": {"http": "http://proxy-server:port", "https": "https://proxy-server:port"}}
chrome_options.add_experimental_option("prefs", prefs)

2. Ignore Certificate Errors

If you’re dealing with SSL errors (such as self-signed certificates), you can instruct Chrome to ignore them.

chrome_options.add_argument("--ignore-certificate-errors")

3. Disable Popup Blocking

Allow popups in Chrome if you want to automate tasks that involve popup windows.

chrome_options.add_argument("--disable-popup-blocking")

4. Disable Password Manager

Disable Chrome’s password manager to prevent saving passwords while testing.

prefs = {"credentials_enable_service": False, "profile.password_manager_enabled": False}
chrome_options.add_experimental_option("prefs", prefs)

5. Disable Notifications

Prevent Chrome from showing notification prompts.

chrome_options.add_argument("--disable-notifications")

6. Use a Specific User Data Directory

Use a custom Chrome profile or data directory, including extensions and settings.

chrome_options.add_argument("user-data-dir=/path/to/custom/profile")

7. Enable/Disable Logging

Enable or disable logging of Chrome’s output to capture browser logs.

chrome_options.add_argument("--enable-logging")
chrome_options.add_argument("--v=1") # Verbosity level (optional)

8. Disable Software Rendering List

Chrome uses a software rendering list to restrict certain graphics operations. You can disable this for testing purposes.

chrome_options.add_argument("--disable-software-rasterizer")

9. Set Default Download Directory

Set a custom directory where files will be downloaded during the test.

prefs = {"download.default_directory": "/path/to/downloads"}
chrome_options.add_experimental_option("prefs", prefs)

10. Enable Experimental Web Platform Features

Enable features that are experimental or under development in Chrome.

chrome_options.add_argument("--enable-experimental-web-platform-features")

11. Disable JavaScript

You can disable JavaScript in Chrome if needed.

prefs = {"profile.managed_default_content_settings.javascript": 2}  # 2 means block JavaScript
chrome_options.add_experimental_option("prefs", prefs)

12. Emulate Device Metrics (Mobile Emulation)

You can emulate a mobile device to test how the site behaves on mobile devices.

mobile_emulation = {"deviceName": "Nexus 5X"}
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)

You can also customize more granular device settings, such as screen size, pixel ratio, etc.

mobile_emulation = {
"deviceMetrics": {
"width": 360,
"height": 640,
"pixelRatio": 3.0
},
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
}
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)

13. Set Custom User-Agent

You can set a custom user-agent string for the browser.

chrome_options.add_argument("user-agent=Your Custom User-Agent String")

14. Use Chrome in a Specific Language

If you need to run Chrome in a different language, you can set the lang preference.

prefs = {"intl.accept_languages": "en-US, en"}
chrome_options.add_experimental_option("prefs", prefs)

15. Disable Site Isolation

Site Isolation is a security feature that isolates websites into separate processes. Disabling it may improve performance in some situations.

chrome_options.add_argument("--disable-site-isolation-trials")

16. Run with Remote Debugging

If you’re debugging the browser or using Chrome DevTools Protocol, you can enable remote debugging on a specific port.

chrome_options.add_argument("--remote-debugging-port=9222")

17. Control Performance Preferences

Chrome provides several performance-related flags, such as controlling the time-to-first-byte, enabling low-latency media playback, etc.

chrome_options.add_argument("--enable-low-latency-media-playback")
chrome_options.add_argument("--force-fieldtest=WebRTC-Force-HW-Video-Encode")

18. Set Custom Chrome Extensions

You can add Chrome extensions during the WebDriver initialization.

chrome_options.add_extension('/path/to/extension.crx')

19. Set Window Size and Position

In addition to specifying a window size, you can also set the position of the Chrome window.

chrome_options.add_argument("window-size=1200x800")
chrome_options.add_argument("window-position=0,0")

20. Use GPU Hardware Acceleration

You can explicitly enable or disable GPU hardware acceleration.

chrome_options.add_argument("--disable-gpu")  # Disable GPU hardware acceleration

21. Enable/Disable Web Security

In some scenarios (like cross-origin testing), you might want to disable certain web security features.

chrome_options.add_argument("--disable-web-security")
chrome_options.add_argument("--allow-running-insecure-content")

22. Use a Specific Chrome Binary

If you need to use a specific Chrome binary (e.g., if you have multiple versions installed), you can specify the path to it.

chrome_options.binary_location = "/path/to/your/chrome/binary"

23. Disable Sync

Prevent Chrome from syncing settings, history, and other data to a Google account.

chrome_options.add_argument("--disable-sync")

24. Headless Mode (Newer Versions)

Recent versions of Chrome use a more robust implementation of headless mode, offering better compatibility with certain web features.

chrome_options.add_argument("--headless=new")

25. Set Custom Extensions Directory

If you want to use multiple extensions or a specific extension directory, you can configure it as follows:

chrome_options.add_argument("--load-extension=/path/to/extension/directory")

Complete Example with Multiple Options:

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()

# Basic options
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--no-sandbox")

# Set preferences
prefs = {
"profile.managed_default_content_settings.images": 2, # Disable images
"download.default_directory": "/path/to/downloads" # Set download directory
}
chrome_options.add_experimental_option("prefs", prefs)

# Mobile emulation
mobile_emulation = {"deviceName": "Nexus 5X"}
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)

# Custom User-Agent
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")

# Initialize the driver with options
driver = webdriver.Chrome(options=chrome_options)

# Open a website
driver.get("https://www.example.com")

# Perform automation tasks...

# Close the browser
driver.quit()

Common Chrome Options

  1. Headless Mode: Run Chrome in headless mode (without UI).
    chrome_options.add_argument("--headless")
  2. Disable Extensions: Disable Chrome extensions during tests.
    chrome_options.add_argument("--disable-extensions")
  3. Maximize Window: Start Chrome maximized.
    chrome_options.add_argument("--start-maximized")
  4. Disable GPU Hardware Acceleration: Disable GPU usage (helpful in headless mode).
    chrome_options.add_argument("--disable-gpu")
  5. Disable Images: Prevent images from loading to speed up tests.
    prefs = {"profile.managed_default_content_settings.images": 2} chrome_options.add_experimental_option("prefs", prefs)
  6. Disable JavaScript: Disable JavaScript execution.
    prefs = {"profile.managed_default_content_settings.javascript": 2} chrome_options.add_experimental_option("prefs", prefs)
  7. Incognito Mode: Launch Chrome in incognito mode.
    chrome_options.add_argument("--incognito")
  8. Proxy Settings: Set the proxy for Chrome.
    chrome_options.add_argument("--proxy-server=http://proxy-server:port")
  9. Disable Notifications: Disable browser notifications.
    chrome_options.add_argument("--disable-notifications")
  10. Ignore Certificate Errors: Ignore SSL certificate errors.
    chrome_options.add_argument("--ignore-certificate-errors")
  11. Disable Password Manager: Disable the Chrome password manager
    prefs = {"credentials_enable_service": False, "profile.password_manager_enabled": False} chrome_options.add_experimental_option("prefs", prefs)
  12. Disable Popup Blocking: Allow popups during testing.
    chrome_options.add_argument("--disable-popup-blocking")
  13. Disable Sync: Disable Chrome sync to Google account.
    chrome_options.add_argument("--disable-sync")
  14. Disable Web Security: Disable web security features for testing cross-origin requests.
    chrome_options.add_argument("--disable-web-security")
  15. Allow Running Insecure Content: Allow insecure content (useful in testing).
    chrome_options.add_argument("--allow-running-insecure-content")
  16. Disable Site Isolation: Disable site isolation security feature.
    chrome_options.add_argument("--disable-site-isolation-trials")
  17. Disable Software Rasterizer: Disable software rendering.
    chrome_options.add_argument("--disable-software-rasterizer")
  18. User-Agent: Set a custom user agent string
    chrome_options.add_argument("user-agent=Custom User-Agent String")
  19. Remote Debugging: Enable Chrome’s remote debugging on a specific port.
    chrome_options.add_argument("--remote-debugging-port=9222")
  20. Disable Logging: Disable logging from Chrome.
    chrome_options.add_argument("--disable-logging")
  21. Set Custom Binary Location: Set a custom Chrome binary location (useful for testing with different versions).
    chrome_options.binary_location = "/path/to/chrome"
  22. Disable WebRTC: Disable WebRTC to prevent leaking local IP address.
    chrome_options.add_argument("--disable-webrtc")
  23. Set Window Size: Set the initial window size.
    chrome_options.add_argument("window-size=1200x800")
  24. Set Window Position: Set the window position.
    chrome_options.add_argument("window-position=0,0")

Experimental Chrome Options

These options are considered experimental and may not be stable in all versions of Chrome:

  1. Mobile Emulation: Emulate mobile devices by passing a device name or custom metrics.
    mobile_emulation = {"deviceName": "Nexus 5X"} chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
  2. Custom Download Directory: Set a custom directory for file downloads.
    prefs = {"download.default_directory": "/path/to/downloads"} chrome_options.add_experimental_option("prefs", prefs)
  3. Set Chrome Preferences: Customize Chrome preferences like download behavior, language, etc.
    prefs = {"intl.accept_languages": "en-US, en"} chrome_options.add_experimental_option("prefs", prefs)
  4. Disable Chrome Extensions: Disable all Chrome extensions.
    chrome_options.add_argument("--disable-extensions")
  5. Experimental Web Platform Features: Enable experimental web platform features.
    chrome_options.add_argument("--enable-experimental-web-platform-features")
  6. Allow Insecure Certificates: Allow usage of invalid SSL certificates.
    chrome_options.add_argument("--allow-insecure-localhost")
  7. Performance Logging: Enable performance logging.
    chrome_options.add_argument("--enable-logging") chrome_options.add_argument("--v=1") # Verbosity level
  8. Force-Field Test for WebRTC: Enable WebRTC for testing purposes.
    chrome_options.add_argument("--force-fieldtest=WebRTC-Force-HW-Video-Encode")
  9. Disable WebGL: Disable WebGL, useful for compatibility testing.
    chrome_options.add_argument("--disable-webgl")
  10. Set Custom Chrome Extensions: Load extensions into Chrome for automation.
    chrome_options.add_extension('/path/to/extension.crx')

More Advanced Options

These options give you advanced control over Chrome’s internal behavior and features:

  1. Experimental Chrome Debugging Features: Enable experimental debugging features.
    chrome_options.add_argument("--enable-logging") chrome_options.add_argument("--v=1") # Verbosity level
  2. Disable Chrome’s Autocomplete Feature: Disable auto-complete in forms.
    prefs = {"profile.autocomplete.enabled": False} chrome_options.add_experimental_option("prefs", prefs)
  3. Disable Chrome’s Password Manager: Disable the Chrome password manager.
    prefs = {"credentials_enable_service": False, "profile.password_manager_enabled": False} chrome_options.add_experimental_option("prefs", prefs)
  4. Disable Blink Features: Disable specific Blink engine features.
    chrome_options.add_argument("--disable-blink-features=AutomationControlled")
  5. Block Popups for Specific URLs: Block popups for specific domains.
    chrome_options.add_argument("--disable-popup-blocking")
  6. Disable Chrome’s Safe Browsing: Disable Chrome’s Safe Browsing feature.
    prefs = {"safebrowsing.enabled": False} chrome_options.add_experimental_option("prefs", prefs)
  7. Disable Content Restrictions for Testing: Disable content restrictions (useful for content testing).
    chrome_options.add_argument("--no-sandbox")
  8. Start Chrome in Full-Screen Mode: Open Chrome in full-screen mode.
    chrome_options.add_argument("--start-fullscreen")
  9. Allow Running Insecure Content: Allow running insecure content in certain contexts.
    chrome_options.add_argument("--allow-running-insecure-content")

Set User Data Directory

This sets a custom user data directory to use a specific profile or settings.

chrome_options.add_argument("user-data-dir=/path/to/user/data")

Complete Example with Multiple Chrome Options:

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()

# Headless mode and start maximized
chrome_options.add_argument("--headless")
chrome_options.add_argument("--start-maximized")

# Disable extension, disable notifications
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-notifications")

# Set custom user-agent
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")

# Enable mobile emulation (e.g., Nexus 5X)
mobile_emulation = {"deviceName": "Nexus 5X"}
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)

# Set custom download directory
prefs = {"download.default_directory": "/path/to/downloads"}
chrome_options.add_experimental_option("prefs", prefs)

# Initialize the driver with options
driver = webdriver.Chrome(options=chrome_options)

# Open a website
driver.get("https://www.example.com")

# Perform automation tasks...

# Close the browser
driver.quit()

No images available.