How do I pass options to the Selenium Chrome driver using Python?

PythonGoogle ChromeSeleniumSelenium Chromedriver

Python Problem Overview


The Selenium documentation mentions that the Chrome webdriver can take an instance of ChromeOptions, but I can't figure out how to create ChromeOptions.

I'm hoping to pass the --disable-extensions flag to Chrome.

Python Solutions


Solution 1 - Python

Found the chrome Options class in the Selenium source code.

Usage to create a Chrome driver instance:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=chrome_options)

Solution 2 - Python

This is how I did it.

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-extensions')

chrome = webdriver.Chrome(chrome_options=chrome_options)

Solution 3 - Python

Code which disable chrome extensions for ones, who uses DesiredCapabilities to set browser flags :

desired_capabilities['chromeOptions'] = {
    "args": ["--disable-extensions"],
    "extensions": []
}
webdriver.Chrome(desired_capabilities=desired_capabilities)

Solution 4 - Python

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--disable-logging')

# Update your desired_capabilities dict withe extra options.
desired_capabilities.update(options.to_capabilities())
driver = webdriver.Remote(desired_capabilities=options.to_capabilities())

Both the desired_capabilities and options.to_capabilities() are dictionaries. You can use the dict.update() method to add the options to the main set.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionk107View Question on Stackoverflow
Solution 1 - Pythonk107View Answer on Stackoverflow
Solution 2 - PythonHassan RazaView Answer on Stackoverflow
Solution 3 - PythonAndriy IvaneykoView Answer on Stackoverflow
Solution 4 - Pythonuser3389572View Answer on Stackoverflow