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

5

9

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.*.

Verla answered 25/7, 2020 at 18:9 Comment(2)
could you intercept the final request to webdriver on execution the webdriver.Chrome and show the request body?Tani
@GennadyZyablitsev I can if you tell me how?Verla
R
4

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)
Ronald answered 24/12, 2020 at 9:47 Comment(0)
C
1

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.

Chiclayo answered 29/9, 2020 at 3:54 Comment(1)
Please provide definite answers with proper code when necessary. If you are not sure, then you can try leaving a comment to the questionBausch
F
1

A bit late but this might work to someone else. Actually, you can set both the debugger address and the download directory, but NOT in the same options. You can use two types of options in initiating the driver.

I have selenium 4.2.0 installed and this works for me on Windows.

from selenium.webdriver import Chrome, ChromeOptions

chrome_driver_path = "Path to your chrome driver"
download_folder = "Download folder path"
preferences = {"download.default_directory" : download_folder}

options = ChromeOptions()
chrome_options = ChromeOptions()

options.add_experimental_option("prefs", preferences)
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = Chrome(chrome_driver_path, options=options, chrome_options=chrome_options)
Flageolet answered 10/1, 2023 at 13:27 Comment(0)
C
0
# Make Sure you have folder in C drive with name download_file
# Please do make the changes according to your need, Thanks

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
import time

# If you want to change the default download location to somewhere else
chromeOptions = Options()
chromeOptions.add_experimental_option("prefs", {"download.default_directory": "C:\\download_file"})

# Google Chrome
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=chromeOptions)

# If you want to download the file in default location
# driver = webdriver.Chrome(ChromeDriverManager().install())

driver.get("http://demo.automationtesting.in/FileDownload.html")

driver.maximize_window()

# Download text file
driver.find_element_by_id("textbox").send_keys("testing download text file")
driver.find_element_by_id("createTxt").click() # Generate file Button
driver.find_element_by_id("link-to-download").click() # Download file

# Download pdf file
driver.find_element_by_id("pdfbox").send_keys("testing download pdf file")
driver.find_element_by_id("createPdf").click() # Generate file Button 
driver.find_element_by_id("pdf-link-to-download").click() # Download file

time.sleep(5)

# Close the Browser
driver.close()
Chui answered 17/2, 2021 at 17:16 Comment(0)
F
0

Add "appium:chromeOptions": {"androidPackage": TARGET_PACKAGE_NAME} to desired_capabilities when create driver. If using UiAutomator2Options , it will be like :

    options = UiAutomator2Options()
    options.platformVersion = '11'
    options.appPackage = TARGET_PACKAGE_NAME

    options.chrome_options = {
        "androidPackage": TARGET_PACKAGE_NAME
    }
    driver = webdriver.Remote(url, options=options)

Franci answered 13/4, 2023 at 8:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.