不同版本的 Selenium 和 WebDriver 的 API 兼容性问题

TypeError: __init__() got an unexpected keyword argument 'executable_path' 是一个常见的错误,通常出现在使用 Selenium 自动化测试工具时。此错误通常是由于不同版本的 Selenium 和 WebDriver 的 API 变化引起的。以下是此问题的详细分析及解决方法。

问题分析

Selenium 是一个用于浏览器自动化的工具,它通过 WebDriver 来控制浏览器。随着 Selenium 的更新,某些参数的使用方式会发生变化。例如,在 Selenium 3 及更早版本中,webdriver.Chrome()__init__ 方法可以接受 executable_path 参数,用于指定 ChromeDriver 的路径。然而,在 Selenium 4 中,webdriver.Chrome() 的初始化方法不再接受 executable_path 参数,而是使用 webdriver.Chrome(service=Service('path_to_driver')) 的方式来指定驱动路径。

因此,当你在 Selenium 4 中仍然使用 executable_path 参数时,就会触发 TypeError: __init__() got an unexpected keyword argument 'executable_path' 错误。

解决方案

要解决这个问题,需要根据所使用的 Selenium 版本来调整代码。以下提供几种不同情况下的解决方案。

方案 1:针对 Selenium 4 的解决方案

Selenium 4 引入了 Service 类来管理浏览器驱动,因此你需要使用 Service 类来传递驱动路径:

复制代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

# 使用 Service 类指定驱动路径
service = Service(executable_path='path_to_chromedriver')
driver = webdriver.Chrome(service=service)

在上述代码中,我们通过 Service 实例化了一个对象 service,然后将其作为参数传递给 webdriver.Chrome()service 参数。这样就可以避免 executable_path 的错误。

方案 2:将 Selenium 降级到 3.x 版本

如果你不想修改现有代码,可以将 Selenium 降级到 3.x 版本,这样可以继续使用 executable_path 参数:

  1. 卸载当前的 Selenium:

    复制代码
    pip uninstall selenium

  2. 安装 Selenium 3.x 版本:

    复制代码
    pip install selenium==3.141.0

安装完成后,你可以继续使用如下代码:

复制代码
from selenium import webdriver

# 继续使用 executable_path 参数
driver = webdriver.Chrome(executable_path='path_to_chromedriver')

此时不会再出现 TypeError 错误。

方案 3:使用 Options 类和 Service

如果你在使用 Options 类来配置浏览器启动选项时,可以结合 OptionsService 类一起使用:

复制代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

# 创建 ChromeOptions 实例
options = Options()
options.add_argument('--headless')  # 无头模式启动

# 创建 Service 实例并指定 ChromeDriver 路径
service = Service(executable_path='path_to_chromedriver')

# 将 service 和 options 传递给 Chrome
driver = webdriver.Chrome(service=service, options=options)

在这个方案中,我们不仅传递了驱动路径,还传递了浏览器启动选项,适合复杂的自动化测试场景。

总结

要解决 TypeError: __init__() got an unexpected keyword argument 'executable_path' 错误,关键在于理解 Selenium 版本的变化:

  • 如果使用 Selenium 4,请使用 Service 类来指定驱动路径。
  • 如果希望继续使用 executable_path 参数,可以将 Selenium 降级到 3.x 版本。
  • 如果在使用浏览器启动选项,请结合 ServiceOptions 类一起使用。

通过上述方案,可以有效解决此类错误并保证代码的兼容性。

相关推荐
Highcharts.js19 小时前
在 Next.js App Router 中使用 Highcharts Stock(完整实战指南 )
开发语言·javascript·ecmascript
TechWayfarer19 小时前
RSAC 2026启示录:从IP归属到IP风险画像,风控系统如何防御住宅代理与AI攻击?
网络·人工智能·python·tcp/ip·ip
摇滚侠19 小时前
Groovy 中如何定义集合
java·开发语言·python
xiaoshuaishuai819 小时前
C# 实现Workstation相关功能
开发语言·windows·c#
游乐码19 小时前
c#Lsit排序
开发语言·c#
财经资讯数据_灵砚智能19 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年4月13日
人工智能·python·信息可视化·自然语言处理·ai编程
hard_coding_wang19 小时前
了解一个Excel批量替换的公式用法:REDUCE + LAMBDA 实现循环替换
开发语言·c#·excel
reasonsummer19 小时前
【教学类-134-01】20260414 Python制作童话故事音频
开发语言·python
文慧的科技江湖19 小时前
光伏储能充电系统PRD功能列表 - 慧知开源充电桩平台
开发语言·开源·netty·慧知开源充电桩平台·开源充电桩平台
Irene199119 小时前
推荐学 Python 的编辑器:PyCharm(附:下载安装教程)
python·编辑器