Selenium初始化Chrome报错:executable_path参数异常(TypeError)
解决Selenium中
executable_path参数错误的方法 错误原因
Selenium 4.x版本起,已移除executable_path初始化参数,改用Service类管理浏览器驱动文件,旧写法会触发类型错误。
修复步骤
1. 导入必要类
从selenium.webdriver.chrome.service导入Service类:
from selenium import webdriver from selenium.webdriver.chrome.service import Service
2. 正确初始化Chrome驱动
情况1:ChromeDriver在脚本目录或已加入环境变量
无需指定路径,直接实例化Service:
service = Service() driver = webdriver.Chrome(service=service)
情况2:手动指定ChromeDriver路径
明确驱动文件位置(Windows系统需加.exe后缀,Linux/macOS无需):
service = Service(executable_path='./chromedriver.exe') driver = webdriver.Chrome(service=service)
3. 验证Selenium版本
运行以下代码确认版本:
import selenium print(selenium.__version__)
若输出为4.x.x,必须使用Service写法;若为3.x.x,原executable_path写法有效,但建议升级至最新版本。
额外提示
- 确保ChromeDriver版本与Chrome浏览器版本完全匹配(你使用的94.0.4606.41对应Chrome 94版本),版本不匹配可能引发其他问题。
- 升级Selenium后若未重启编辑器/终端,可能环境未更新,重启后再运行脚本。
内容的提问来源于stack exchange,提问作者Meta
相关产品推荐
相关产品推荐

