类型错误:WebDriver.__init__() 获得了参数“选项”的多个值

问题描述 投票:0回答:2

错误是:

TypeError: WebDriver.__init__() got multiple values for argument 'options'

`

代码是:

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

browser = webdriver.Chrome(r'/usr/bin/chromedriver', options=chrome_options)

这是错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-5-9a7e59e392ae> in <cell line: 6>()
      4 chrome_options.add_argument('--headless')
      5 
----> 6 browser = webdriver.Chrome(r'/usr/bin/chromedriver', options=chrome_options)

TypeError: WebDriver.__init__() got multiple values for argument 'options'
python python-3.x selenium-webdriver selenium-chromedriver webdriver
2个回答
28
投票

这是由于

selenium
4.10.0
的变化所致: https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8176e3ccd8e

Changes_in_selenium_4_10_0

请注意,第一个参数不再是

executable_path
,而是
options
。 (这就是为什么它抱怨你两次传递它。)

如果您想传递

executable_path
,您现在必须使用
service
参数。

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

service = Service(executable_path=r'/usr/bin/chromedriver')
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()

1
投票

#selenium 驱动程序不起作用的工作解决方案

from selenium import webdriver

from selenium.webdriver.chrome.service import Service

请交叉检查您是否在 Chromedriver 路径末尾添加 .exe

service = Service(executable_path=r"..\chromedriver.exe") 

driver = webdriver.Chrome(service=service)

#它对我有用,希望 selenium 版本 4.1 chrome 116 最新版

© www.soinside.com 2019 - 2024. All rights reserved.