ChromeDriver与Chrome版本不兼容问题求助(Selenium v4.6)
错误信息
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 114
Current browser version is 116.0.5845.97 with binary path C:\Program Files\Google\Chrome\Application\chrome.exe
问题代码
from selenium import webdriver import time from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By class ChromeAuto: def __init__(self): self.options = webdriver.ChromeOptions() self.options.add_experimental_option('excludeSwitches', ['enable-logging']) self.service = Service() self.chrome = webdriver.Chrome(service=self.service, options=self.options) self.chrome.implicitly_wait(20)
解决方案
尽管Selenium 4.6.0及以上自带ChromeDriver自动管理功能,但缓存或残留文件可能导致版本不匹配,按以下步骤处理:
确认并升级Selenium版本
执行命令查看当前版本:pip show selenium,若版本低于4.6.0,执行升级:pip install --upgrade selenium清理旧ChromeDriver缓存与残留
删除系统中手动放置的旧ChromeDriver文件,同时清理Selenium缓存目录:
Windows路径为C:\Users\<你的用户名>\.cache\selenium,删除该目录下的chromedriver文件夹强制重新匹配版本
完成上述操作后重新运行代码,Selenium会自动下载与Chrome 116匹配的ChromeDriver手动指定ChromeDriver(备选方案)
若自动管理仍失效,下载对应Chrome 116版本的ChromeDriver,修改代码中Service的路径:self.service = Service(executable_path=r"C:\你的chromedriver路径\chromedriver.exe")
内容的提问来源于stack exchange,提问作者Gabriel Passos

