Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot parse capability: goog:chromeOptions from invalid argument: unrecognized chrome option: prefs

I'm trying to use the Selenium WebDriver and ChromeDriver to connect to an existing session and set the download folder. For some reason, it always fails and throws the following errors:

>>> from selenium import webdriver
>>>
>>> chromeOptions = webdriver.ChromeOptions()
>>>
>>> download_folder = "D:\\Test"
>>> preferences = {
... "download.default_directory" : download_folder
... }
>>> chromeOptions.add_experimental_option("prefs", preferences)
>>> chromeOptions.add_experimental_option("debuggerAddress", "127.0.0.1:1111")
>>>
>>> chromeOptions.to_capabilities()
{'browserName': 'chrome', 'version': '', 'platform': 'ANY', 'goog:chromeOptions': {'prefs': {'download.default_directory': 'D:\\Test'}, 'debuggerAddress': '127.0.0.1:1111', 'extensions': [], 'args': []}}
>>>
>>> driver_Chrome = "D:\\chromedriver.exe"
>>> driver_Web = webdriver.Chrome(executable_path=driver_Chrome, chrome_options=chromeOptions)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 76, in __init__
    RemoteWebDriver.__init__(
  File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\username\AppData\Local\Programs\Python\Python38\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

 

However, if I remove either one of the chromeOptions.add_experimental_option lines then it works fine, which doesn't make sense to me:

>>> from selenium import webdriver
>>>
>>> chromeOptions = webdriver.ChromeOptions()
>>>
>>> download_folder = "D:\\Test"
>>> preferences = {
... "download.default_directory" : download_folder
... }
>>> chromeOptions.add_experimental_option("prefs", preferences)
>>> # chromeOptions.add_experimental_option("debuggerAddress", "127.0.0.1:1111")
>>>
>>> chromeOptions.to_capabilities()
{'browserName': 'chrome', 'version': '', 'platform': 'ANY', 'goog:chromeOptions': {'prefs': {'download.default_directory': 'D:\\Test'}, 'extensions': [], 'args': []}}
>>>
>>> driver_Chrome = "D:\\chromedriver.exe"
>>> driver_Web = webdriver.Chrome(executable_path=driver_Chrome, chrome_options=chromeOptions)
<stdin>:1: DeprecationWarning: use options instead of chrome_options

DevTools listening on ws://127.0.0.1:4523/devtools/browser/e34332d4-50f0-4419-b9fb-37ede5f5602e
>>>

>>> from selenium import webdriver
>>>
>>> chromeOptions = webdriver.ChromeOptions()
>>>
>>> download_folder = "D:\\Test"
>>> preferences = {
... "download.default_directory" : download_folder
... }
>>> # chromeOptions.add_experimental_option("prefs", preferences)
>>> chromeOptions.add_experimental_option("debuggerAddress", "127.0.0.1:1111")
>>>
>>> chromeOptions.to_capabilities()
{'browserName': 'chrome', 'version': '', 'platform': 'ANY', 'goog:chromeOptions': {'debuggerAddress': '127.0.0.1:1111', 'extensions': [], 'args': []}}
>>>
>>> driver_Chrome = "D:\\chromedriver.exe"
>>> driver_Web = webdriver.Chrome(executable_path=driver_Chrome, options=chromeOptions)
>>>

 

I've expanded out the "capabilities" from each output and they seem fine to me:

{
    'browserName': 'chrome',
    'version': '',
    'platform': 'ANY',
    'goog:chromeOptions': {
        'prefs': {
            'download.default_directory': 'D:\\Test'
        },
        'debuggerAddress': '127.0.0.1:1111',
        'extensions': [],
        'args': []
    }
}

{
    'browserName': 'chrome',
    'version': '',
    'platform': 'ANY',
    'goog:chromeOptions': {
        'prefs': {
            'download.default_directory': 'D:\\Test'
        },
        'extensions': [],
        'args': []
    }
}

 

My environment is as follows:

  • Windows 10 version 2004
  • Python 64-bit version 3.8.4 (latest)
  • Selenium version 3.141.0 (latest)
  • Google Chrome 64-bit version 84.0.4147.89 (latest), opened with argument --remote-debugging-port=1111
  • ChromeDriver version 84.0.4147.30

Nothing I've found online has helped or been regarding this particular scenario.

 

Update 2020/09/07 23:26: Same problem with versions 85.0.4183.*.

like image 411
mythofechelon Avatar asked Oct 16 '22 01:10

mythofechelon


2 Answers

Disclaimer: This is a workaround that suits my needs. This may not help everyone.

I wanted to capture as pdf, data from a website that was secured by SSO, and it proved cumbersome and time consuming to figure out how to authenticate to view the desired page.

So used debuggerAddress to login to running chrome instance where I had already logged into SSO.

But faced with same issue as OP, when trying to invoke add_experimental_option twice, once for setting debuggerAddress, another for setting print preferences in 'prefs'.

Solution

  1. Only invoke add_experimental_option once to set prefs and remove invocation for setting debuggerAddress.
  2. Add sleep time.sleep(60) for atleast one minute
  3. In this time, login to SSO in chrome instance started by script.
  4. Once sleep completes your selenium script can navigate to desired secure website using driver.get(url)
like image 67
arvindkgs Avatar answered Oct 19 '22 00:10

arvindkgs


Maybe you cannot set both, debuggerAddress AND download.default_directory, because you are not allowed to make changes in the download directory on a running chrome instance.

like image 24
user14358676 Avatar answered Oct 19 '22 00:10

user14358676