Selenium报错SessionNotCreatedException:ChromeDriver版本不匹配求助
解决chromedriver-autoinstaller仍出现Chrome版本不匹配的SessionNotCreatedException问题
问题根源
你的代码里手动添加了旧ChromeDriver的系统路径,导致Python优先调用114版本的旧驱动,而Chrome浏览器已自动更新到119版本,从而触发版本不匹配报错。另外chromedriver-autoinstaller默认不会强制覆盖旧驱动,也会导致问题随机出现。
修复步骤
- 移除旧驱动路径的手动指定:删掉
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')这行,避免系统优先加载旧驱动。 - 强制安装匹配的驱动:让chromedriver-autoinstaller返回驱动路径并传给Service,确保使用最新安装的适配版本。
- 可选:验证版本一致性:添加代码检查Chrome和驱动版本,确认两者匹配。
修改后的完整代码
!pip install --upgrade chromedriver-autoinstaller import sys from selenium.webdriver.chrome.service import Service import time import pandas as pd from bs4 import BeautifulSoup from selenium import webdriver import chromedriver_autoinstaller # 配置Chrome选项 chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') # 关闭GUI chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') # 强制安装并获取匹配的ChromeDriver路径 driver_path = chromedriver_autoinstaller.install(True) # True表示覆盖现有驱动 service = Service(driver_path) # 验证版本(可选步骤) from selenium.webdriver.chrome.version import get_chrome_version print(f"Chrome版本: {get_chrome_version()}") print(f"ChromeDriver路径: {driver_path}") # 设置目标URL url = "put-url-here-to-scrape" # 初始化浏览器驱动 driver = webdriver.Chrome(options=chrome_options, service=service)
额外排查建议
- 如果问题仍存在,手动删除系统中残留的旧驱动文件:
sudo rm /usr/lib/chromium-browser/chromedriver - 确保Chrome浏览器未被限制更新,保持版本与驱动同步
内容的提问来源于stack exchange,提问作者Skanda Vyas
相关产品推荐
相关产品推荐

