Selenium未使用指定ChromeDriver,仍调用系统默认版本问题
解决Selenium指定ChromeDriver后仍调用默认Chrome版本的问题
问题场景
需使用Chrome 114版本获取网页源码,已下载对应版本的ChromeDriver并通过Service配置路径,但程序仍调用系统默认的Chrome 116,抛出SessionNotCreatedException,提示当前ChromeDriver仅支持Chrome 114版本。
原因分析
仅指定ChromeDriver路径无法约束Selenium调用的浏览器版本——Selenium默认会查找系统默认安装的Chrome二进制文件,不会自动匹配你需要的旧版本Chrome,必须手动指定旧版本Chrome的执行文件路径。
解决步骤
- 找到你安装的Chrome 114的二进制文件路径(Mac系统示例路径:
/Applications/Google Chrome 114.app/Contents/MacOS/Google Chrome,请替换为实际路径) - 通过
ChromeOptions对象的binary_location属性指定该路径 - 将配置好的
options传入webdriver.Chrome的初始化参数中
修改后的完整代码
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options # 配置ChromeDriver路径 chrome_service = Service("/Users/*/Downloads/chromedriver_mac64/chromedriver") # 配置Chrome 114的二进制文件路径 chrome_options = Options() chrome_options.binary_location = "/Applications/Google Chrome 114.app/Contents/MacOS/Google Chrome" # 替换为你的实际路径 # 初始化驱动,同时传入service和options driver = webdriver.Chrome(service=chrome_service, options=chrome_options) url = "https://google.com" driver.get(url) driver.implicitly_wait(10) website_source = driver.page_source print(website_source) driver.quit()
验证要点
- 确保Chrome 114已正确安装,路径无拼写错误
- ChromeDriver版本必须与Chrome 114主版本号完全一致
- 若仍报错,检查Chrome 114是否能单独正常启动
内容的提问来源于stack exchange,提问作者llama-for3ver
相关产品推荐
相关产品推荐

