You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用webdriver.Chrome报错:WebDriver.__init__()参数'options'传值重复

问题解决:TypeError: WebDriver.init() got multiple values for argument 'options'

错误原因

在Selenium 4.x及以上版本中,webdriver.Chrome的构造函数参数规则发生了变化:不再支持直接将驱动可执行路径作为第一个位置参数传入。原代码中同时传递了位置参数(驱动路径)和关键字参数options=options,导致参数解析冲突,触发该错误。

修复步骤

  1. 导入Service类:从selenium.webdriver.chrome.service模块导入Service,用于管理Chrome驱动服务。
  2. 用Service包装驱动路径:将ChromeDriverManager().install()返回的驱动路径传入Service实例。
  3. 正确初始化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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 05:58:33