How to pass Desired Capabilities to Undetected Chromedriver with Selenium Python?
Asked Answered
C

3

6

I'm using the Python package Undetected Chromedriver as I need to be able to log into a Google account with the webdriver, and I want to pass the options {"credentials_enable_service": False, "profile.password_manager_enabled": False} to the driver so that it doesn't bring up the popup to save the password. I was trying to pass those options using:

import undetected_chromedriver.v2 as uc

uc_options = uc.ChromeOptions()
uc_options.add_argument("--start-maximized")
uc_options.add_experimental_option("prefs", {"credentials_enable_service": False, "profile.password_manager_enabled": False})

driver2 = uc.Chrome(options=uc_options)

The argument --start-maximized works perfectly fine, and if I run the code with just that it starts maximised as intended. However, when adding the experimental options and running the code it returns the error:

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: cannot parse capability: goog:chromeOptions
from invalid argument: unrecognized chrome option: prefs

So I thought that I would try to pass the arguments as Desired Capabilities instead, making the code:

import undetected_chromedriver.v2 as uc

uc_options = uc.ChromeOptions()
uc_options.add_argument("--start-maximized")
uc_options.add_experimental_option("prefs", {"credentials_enable_service": False, "profile.password_manager_enabled": False})
uc_caps = uc_options.to_capabilities()

driver2 = uc.Chrome(desired_capabilities=uc_caps)

While this code runs and doesn't generate any errors, it also doesn't do anything at all. The password popup still shows up, and the driver doesn't even start maximised, despite the fact that the latter part worked as an option.

So my question is: how do I correctly pass Desired Capabilities to the Undetected Chromedriver? Or, alternatively: how do I correctly pass the Experimental Options to the Undetected Chromedriver?

Cyrie answered 15/5, 2021 at 20:41 Comment(10)
Have you tried 'prefs', {'credentials_enable_service': False, 'profile': {'password_manager_enabled': False}Eocene
Why don't you simply click on the popup?Convery
You should be able to set window size in driver... (in Java it's something like: driver.manage().window().setPosition... setSize... etc...)Ernestoernestus
I think you should be setting it as an option. (You should be able to mix both options and capabilities btw... but set them separately.) You sure that values for those settings are bools? Many will have 0,1,2 instead.Ernestoernestus
@Eocene I just tried that, but unfortunately it gives invalid sytanx no matter which way I try.Cyrie
@pcalkins I have tried passing them as an option, but that results in the unrecognised chrome option error shown above. And every way I've seen from other posts of how to pass those arguments to the regular chromedriver uses that exact syntax, so I'm fairly certain they're bools.Cyrie
maybe try removing the start maximized option. I think your undetected chromedriver already sets that. (though it uses: add_argument("start-maximized")... for some reason)Ernestoernestus
I've also seen it mentioned before that add_experimental should only be used once. It's used in the undetected chromedriver before you use it. (I think it has something to do with the way it writes to preferences file? Or overrides it...)Ernestoernestus
the more I look at it, it seems like your code is right and the undectected chromedriver code is wrong. I think it's borking the preferences file. It's also very doubtful that any of this would bypass Google's detection.Ernestoernestus
@pcalkins Yeah, I don't think it's possible to pass those options to the Undetected Chromdriver unfortunately. What I ended up doing was having it go to the settings page and disable the option from there, which is a little bit of a hassle but at least it works. Thanks for the help anyway!Cyrie
N
2

Earlier this year, there were changes made to fix this issue and allow preferences:

https://github.com/ultrafunkamsterdam/undetected-chromedriver/issues/524

See https://github.com/ultrafunkamsterdam/undetected-chromedriver/commit/487969811851be6bcf6e3c55c8fc0d471940c6c3 for the commit.

That made important updates to https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/undetected_chromedriver/options.py in order to handle preferences.

Upgrade to a newer version to fix your issue.

Nelan answered 9/9, 2022 at 1:31 Comment(0)
F
3

Undetected Chromedriver starts the webdriver service and Chrome as a normal browser with arguments, and then attaches a webdriver. Note that experimental preferences cannot be used on an already running instance.

As a workaround, you can use the Undetected Chromedriver patcher to modify the chromedriver and then use it. However, you need to check if chrome is still undetectable for your website. There are additional settings done for a headless browser, so if you need headless, check the source code.

import undetected_chromedriver.v2 as uc
from selenium import webdriver

patcher = uc.Patcher()
patcher.auto()

options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option(
    "prefs", {"credentials_enable_service": False, "profile.password_manager_enabled": False})

with webdriver.Chrome(options=options, executable_path=patcher.executable_path) as driver:
    driver.get("")
Fula answered 22/5, 2021 at 13:49 Comment(2)
I just tried that but it still gives the unrecognized chrome option: prefs error as shown above, unfortunately.Cyrie
This just doesn't work for me. Chrome opens and them closes.Jermyn
T
2

Try using Options() instead of ChromeOptions() with @Sers answer

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option(
"prefs", {"credentials_enable_service": False, "profile.password_manager_enabled": False})
Tracheid answered 23/5, 2021 at 3:44 Comment(2)
Just tried this and it gives the error ImportError: cannot import name 'Options' from 'undetected_chromedriver.v2'Cyrie
Try using this with undetected_chromedriver v1 it is available on githubTracheid
N
2

Earlier this year, there were changes made to fix this issue and allow preferences:

https://github.com/ultrafunkamsterdam/undetected-chromedriver/issues/524

See https://github.com/ultrafunkamsterdam/undetected-chromedriver/commit/487969811851be6bcf6e3c55c8fc0d471940c6c3 for the commit.

That made important updates to https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/undetected_chromedriver/options.py in order to handle preferences.

Upgrade to a newer version to fix your issue.

Nelan answered 9/9, 2022 at 1:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.