简体   繁体   中英

How to pass Desired Capabilities to Undetected Chromedriver with Selenium Python?

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?

Undetected Chromedriver start webdriver service and Chrome as a normal browser with arguments, and after attaches a webdriver. Probably experimental preferents cannot be used on already running instance.

As workaround you can use Undetected Chromedriver patcher to modify the chromedriver and then use the it. But you need to check if the chrome is steal not detectable for your website. There're additional settings done for 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("")

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})

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM