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.*.
webdriver.Chrome
and show the request body? – Tani