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

如何避免Selenium频繁遭遇ChromeDriver版本错误并自动匹配对应版本?

自动匹配ChromeDriver与Chrome版本的解决方案(适配Selenium 4.18.0)

方案1:使用webdriver-manager自动管理

webdriver-manager完全适配Selenium 4.18.0,若之前安装失败,尝试用以下命令重新安装:

pip install webdriver-manager

修改你的代码,替换原有的Service初始化逻辑,让webdriver-manager自动检测Chrome版本并下载对应ChromeDriver:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

# 设置浏览器选项
option = webdriver.ChromeOptions()
option.add_argument('--headless')
option.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")

# 自动获取对应版本的ChromeDriver路径
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=option)

方案2:使用Selenium 4.6+内置的驱动管理

Selenium 4.6及以上版本内置了驱动自动管理功能,无需额外安装第三方包,直接简化代码即可:

from selenium import webdriver
from selenium.webdriver.common.by import By

# 设置浏览器选项
option = webdriver.ChromeOptions()
option.add_argument('--headless')
option.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")

# Selenium会自动检测Chrome版本并匹配对应Driver
driver = webdriver.Chrome(options=option)

说明

两种方案都会自动检测本地Chrome的版本,下载并使用完全匹配的ChromeDriver,避免手动下载时的版本匹配错误。每月运行代码时,只要Chrome自动更新,脚本会自动同步获取对应版本的驱动,无需手动干预。

内容的提问来源于stack exchange,提问作者cbirch

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 10:54:56