调用webdriver.Chrome报错:WebDriver.__init__()参数'options'传值重复
问题解决:TypeError: WebDriver.init() got multiple values for argument 'options'
错误原因
在Selenium 4.x及以上版本中,webdriver.Chrome的构造函数参数规则发生了变化:不再支持直接将驱动可执行路径作为第一个位置参数传入。原代码中同时传递了位置参数(驱动路径)和关键字参数options=options,导致参数解析冲突,触发该错误。
修复步骤
- 导入
Service类:从selenium.webdriver.chrome.service模块导入Service,用于管理Chrome驱动服务。 - 用
Service包装驱动路径:将ChromeDriverManager().install()返回的驱动路径传入Service实例。 - 正确初始化Chrome驱动:将
service和options作为关键字参数传入webdriver.Chrome。
修复后的代码
from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager from selenium import webdriver options = Options() options.add_experimental_option("excludeSwitches", ["enable-logging"]) options.add_argument("--profile-directory=Default") options.add_argument("--user-data-dir=/var/tmp/chrome_user_data") # 用Service包装驱动路径,传入Chrome构造函数 driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
旧版本兼容说明
如果使用的是Selenium 3.x版本,可通过显式指定executable_path参数解决:
# Selenium 3.x兼容写法 driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options)
内容的提问来源于stack exchange,提问作者CS22B1026_Mani bharathi
相关产品推荐
相关产品推荐

