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:
--remote-debugging-port=1111
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.*.
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
add_experimental_option
once to set prefs and remove invocation for setting debuggerAddress
.time.sleep(60)
for atleast one minutedriver.get(url)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With