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

Posted on

Question :

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

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.

Asked By: k107

||

Answer #1:

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)
Answered By: k107

Answer #2:

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)
Answered By: Hassan Raza

Answer #3:

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)
Answered By: Andriy Ivaneyko

Answer #4:

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.

Answered By: user3389572

Leave a Reply

Your email address will not be published. Required fields are marked *