pythonseleniumselenium-webdriverselenium-chromedriverchrome-options

Selenium chrome use multiple add_experimental_option


I want to use 2 experimental options on my selenium chromedriver project, but I don't get enough information about the "add_experimental_option" function, I want to use 2 experimental options "prefs" and "debuggerAddress", and when I try to run it, it gets error, I can only use one of them, how can I use both of them?

opt = Options()
opt.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
opt.add_experimental_option("prefs", { \
    "profile.managed_default_content_settings.images": 2
})

error messages

Traceback (most recent call last):
  File "exampleselenium.py", line 28, in <module>
    driver = webdriver.Chrome(executable_path="chromedriver.exe",options=opt,service_log_path='NUL')
  File "C:\Users\Faraaz\anaconda3\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 76, in __init__
    RemoteWebDriver.__init__(
  File "C:\Users\Faraaz\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\Faraaz\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\Faraaz\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Faraaz\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: cannot parse capability: goog:chromeOptions
from invalid argument: unrecognized chrome option: prefs

Solution

  • ChromeOptions objects

    You can add both the experimental_option as follows:

    opt = webdriver.ChromeOptions()
    prefs = {"profile.managed_default_content_settings.images" : 2}
    opt.add_experimental_option("prefs", prefs)
    opt.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
    driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', options=opt, service_log_path='NUL')