Last modified: Jan 02, 2024 By Alexander Williams

Python Selenium: add_experimental_option - Examples

Example 1: Disable Chrome GPU Hardware Acceleration


from selenium import webdriver

# Create a ChromeOptions object
chrome_options = webdriver.ChromeOptions()

# Disable GPU hardware acceleration
chrome_options.add_experimental_option('useAutomationExtension', False)
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])

# Create a Chrome driver with the configured ChromeOptions
driver = webdriver.Chrome(options=chrome_options)

# Perform automation tasks with the customized browser
driver.get('https://example.com')

Output: N/A

Example 2: Set Firefox Browser Preferences


from selenium import webdriver

# Create a FirefoxOptions object
firefox_options = webdriver.FirefoxOptions()

# Set browser preferences for Firefox
firefox_options.add_experimental_option('prefs', {
    'browser.download.folderList': 2,
    'browser.download.manager.showWhenStarting': False,
    'browser.download.dir': '/path/to/download/directory',
    'browser.helperApps.neverAsk.saveToDisk': 'application/pdf'
})

# Create a Firefox driver with the configured FirefoxOptions
driver = webdriver.Firefox(options=firefox_options)

# Perform automation tasks with the customized browser
driver.get('https://example.com')

Output: N/A