ChromeDriver版本不兼容:如何匹配Chrome 116的正确驱动?
ChromeDriver版本兼容问题解决方案
问题背景
我编写了一段Python脚本,使用undetected_chromedriver加载本地Chrome配置文件打开Gmail,目的是避免重复登录并通过键盘快捷键激活扩展。脚本代码如下:
import undetected_chromedriver as uc from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service import time # Set up Chrome WebDriver service = Service() options = Options() options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") options.add_argument("--profile-directory=Default") options.add_argument("--user-data-dir=C:\\Users\\athar\\AppData\\Local\\Google\\Chrome\\User Data\\") driver = uc.Chrome(service = service, options = options) driver.get("https://gmail.com/") time.sleep(10) # Close the browser driver.quit()
运行脚本时出现版本不兼容错误,完整错误信息:
Traceback (most recent call last): File "d:\programming\Python\Freelance\ExtensionAutomation\test.py", line 16, in <module> driver = uc.Chrome(service = service, options = options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\athar\AppData\Local\Programs\Python\Python311\Lib\site-packages\undetected_chromedriver\__init__.py", line 466, in __init__ super(Chrome, self).__init__( File "C:\Users\athar\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__ super().__init__( File "C:\Users\athar\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 56, in __init__ super().__init__( File "C:\Users\athar\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 206, in __init__ self.start_session(capabilities) File "C:\Users\athar\AppData\Local\Programs\Python\Python311\Lib\site-packages\undetected_chromedriver\__init__.py", line 724, in start_session super(selenium.webdriver.chrome.webdriver.WebDriver, self).start_session( File "C:\Users\athar\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 290, in start_session response = self.execute(Command.NEW_SESSION, caps)["value"] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\athar\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 345, in execute self.error_handler.check_response(response) File "C:\Users\athar\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: 未知错误:无法连接到127.0.0.1:54613的Chrome 来自会话未创建:此版本的ChromeDriver仅支持Chrome版本114 当前浏览器版本为116.0.5845.110 Stacktrace: Backtrace: GetHandleVerifier [0x00FDA813+48355] (No symbol) [0x00F6C4B1] (No symbol) [0x00E75358] (No symbol) [0x00E961AC] (No symbol) [0x00E906B4] (No symbol) [0x00E90491] (No symbol) [0x00EC0C55] (No symbol) [0x00EC093C] (No symbol) [0x00EBA536] (No symbol) [0x00E982DC] (No symbol) [0x00E993DD] GetHandleVerifier [0x0123AABD+2539405] GetHandleVerifier [0x0127A78F+2800735] GetHandleVerifier [0x0127456C+2775612] GetHandleVerifier [0x010651E0+616112] (No symbol) [0x00F75F8C] (No symbol) [0x00F72328] (No symbol) [0x00F7240B] (No symbol) [0x00F64FF7] BaseThreadInitThunk [0x768D7D59+25] RtlInitializeExceptionChain [0x77C6B79B+107] RtlClearBits [0x77C6B71F+191]
如何获取与Chrome 116兼容的正确ChromeDriver版本?需要下载什么文件,以及如何将驱动路径传递给WebDriver函数?
解决方案
1. 获取对应版本的ChromeDriver
ChromeDriver版本必须与本地Chrome的主版本号完全匹配(你的Chrome是116.0.5845.110,需下载116.x.x.x版本的ChromeDriver):
- 下载对应平台的压缩包:
- Windows系统:
chromedriver_win32.zip - macOS Intel芯片:
chromedriver_mac64.zip;M系列芯片:chromedriver_mac_arm64.zip - Linux系统:
chromedriver_linux64.zip
- Windows系统:
- 解压后得到可执行文件:Windows为
chromedriver.exe,macOS/Linux为chromedriver
2. 传递驱动路径给WebDriver
修改脚本,将解压后的ChromeDriver路径传入WebDriver,有两种方式:
方式一:通过Selenium的Service类指定
import undetected_chromedriver as uc from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service import time # 替换为你解压后的ChromeDriver实际路径 driver_path = "C:\\your\\path\\to\\chromedriver.exe" # Windows示例 # driver_path = "/Users/yourname/path/to/chromedriver" # macOS/Linux示例 service = Service(driver_path) options = Options() options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") options.add_argument("--profile-directory=Default") options.add_argument("--user-data-dir=C:\\Users\\athar\\AppData\\Local\\Google\\Chrome\\User Data\\") driver = uc.Chrome(service=service, options=options) driver.get("https://gmail.com/") time.sleep(10) driver.quit()
方式二:直接给undetected_chromedriver指定executable_path
import undetected_chromedriver as uc from selenium.webdriver.chrome.options import Options import time options = Options() options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") options.add_argument("--profile-directory=Default") options.add_argument("--user-data-dir=C:\\Users\\athar\\AppData\\Local\\Google\\Chrome\\User Data\\") # 替换为你的ChromeDriver实际路径 driver = uc.Chrome(executable_path="C:\\your\\path\\to\\chromedriver.exe", options=options) driver.get("https://gmail.com/") time.sleep(10) driver.quit()
额外提示
- 确保ChromeDriver与Chrome的主版本号严格一致,避免小版本差异导致的兼容问题
- 运行脚本时不要同时打开本地Chrome浏览器,否则会触发配置文件占用冲突
- 也可以尝试升级
undetected_chromedriver到最新版本,部分版本会自动适配并下载对应ChromeDriver
内容的提问来源于stack exchange,提问作者harveyspecter
相关产品推荐
相关产品推荐

