硒铬使用多个add_experimental_option



我想在我的硒chromedriver项目上使用两个实验选项,但我没有得到足够的关于"add_experimental_option"函数,我想使用2个实验选项";prefs";以及";debuggerAddress";,当我尝试运行它时,它会出错,我只能使用其中一个,我怎么能同时使用它们呢?

opt = Options()
opt.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
opt.add_experimental_option("prefs", { 
"profile.managed_default_content_settings.images": 2
})

错误消息

Traceback (most recent call last):
File "exampleselenium.py", line 28, in <module>
driver = webdriver.Chrome(executable_path="chromedriver.exe",options=opt,service_log_path='NUL')
File "C:UsersFaraazanaconda3libsite-packagesseleniumwebdriverchromewebdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "C:UsersFaraazanaconda3libsite-packagesseleniumwebdriverremotewebdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:UsersFaraazanaconda3libsite-packagesseleniumwebdriverremotewebdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:UsersFaraazanaconda3libsite-packagesseleniumwebdriverremotewebdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:UsersFaraazanaconda3libsite-packagesseleniumwebdriverremoteerrorhandler.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

ChromeOptions对象

  • prefs〔type:dictionary〕:一个字典,每个条目都由首选项的名称及其值组成。这些首选项仅应用于正在使用的用户配置文件。

  • debuggerAddress[type:string]:要连接的Chrome调试器服务器的地址,格式为<hostname/ip:port>,例如"127.0.0.1:38947">

您可以按如下方式添加两个experimental_option

opt = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images" : 2}
opt.add_experimental_option("prefs", prefs)
opt.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(executable_path=r'C:pathtochromedriver.exe', options=opt, service_log_path='NUL')

相关内容

  • 没有找到相关文章