Python+Selenium+ChromeDriver报错:无法检测Chrome版本求助
WSL环境下Selenium ChromeDriver版本检测问题解决办法
问题背景
在WSL环境中使用Selenium抓取动态内容,已安装ChromeDriver 114.0.5735.90,Windows端Chrome已更新至114.0.5735.199,路径配置正确,但运行代码时出现报错:
The version of chrome cannot be detected. Trying with latest driver version
已尝试重新安装WSL中的ChromeDriver,问题仍存在。
核心原因
Selenium默认会检测WSL系统内的Chrome浏览器版本,但当前仅在Windows端安装了Chrome,WSL内无Chrome,导致版本检测失败;同时ChromeDriver版本(114.0.5735.90)与Windows端Chrome小版本(114.0.5735.199)存在差异,也会加剧匹配问题。
解决方法
1. 在WSL内安装对应版本的Chrome
以Ubuntu系WSL为例,执行以下命令安装Chrome:
sudo apt update && sudo apt install -y wget curl wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb sudo dpkg -i google-chrome-stable_current_amd64.deb sudo apt install -f -y
安装完成后执行google-chrome --version确认版本,确保与Windows端Chrome大版本一致(均为114.x),小版本差异可忽略。
2. 直接指定Windows端Chrome的二进制文件路径
无需在WSL安装Chrome,通过配置Selenium选项指向Windows的Chrome:
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options options = Options() # Windows Chrome的WSL映射路径,根据实际安装位置调整 options.binary_location = '/mnt/c/Program Files/Google/Chrome/Application/chrome.exe' webdriver_service = Service('~/chromedriver') driver = webdriver.Chrome(service=webdriver_service, options=options) driver.get('http://www.google.com/')
3. 使用webdriver-manager自动匹配版本
安装webdriver-manager包自动管理ChromeDriver版本,无需手动下载:
pip install webdriver-manager
修改代码为:
from selenium import webdriver from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager # 自动检测Chrome版本并安装对应ChromeDriver driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) driver.get('http://www.google.com/')
内容的提问来源于stack exchange,提问作者Jerry
相关产品推荐
相关产品推荐

