
本文详解 Selenium 升级至 4.10+ 后因弃用手动驱动管理、版本不匹配等问题导致 webdriver.Chrome() 启动失败的典型场景,并提供精简可靠的新版初始化方案及排错方法。
本文详解 selenium 升级至 4.10+ 后因弃用手动驱动管理、版本不匹配等问题导致 `webdriver.chrome()` 启动失败的典型场景,并提供精简可靠的新版初始化方案及排错方法。
随着 Selenium 4.10 版本的发布,其内置了自动化的浏览器驱动管理工具 —— selenium-manager,正式取代了开发者手动下载、指定 chromedriver.exe 路径的做法。若你仍沿用旧代码显式传入 Service(executable_path=...),在新版中极易触发 WebDriverException、SessionNotCreatedException 或静默卡死等异常,尤其当 Chrome 浏览器与驱动版本不一致时,问题会进一步加剧。
✅ 推荐做法:完全移除手动 Service 配置(Selenium ≥ 4.10)
新版 Selenium 可自动下载、缓存并匹配兼容的 ChromeDriver,只需确保系统已安装 Chrome 浏览器(v115+),且 chromedriver 不再需要手动维护:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
# 可选:无头模式或禁用自动化提示(提升稳定性)
# options.add_argument("--headless")
# options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(options=options) # ✅ 无需 Service 参数
driver.get("https://www.google.com")
print("✅ 页面加载成功,当前标题:", driver.title)
driver.quit()
? 提示:首次运行会自动触发
selenium-manager下载适配的驱动(需联网),后续复用本地缓存,无需重复操作。
⚠️ 常见问题排查清单
| 问题现象 | 可能原因 | 解决方案 |
|---|---|---|
SessionNotCreatedException: session not created: This version of ChromeDriver only supports Chrome version XX |
Chrome 与 chromedriver 版本严重不匹配 | 访问 chrome://settings/help 查看 Chrome 版本;卸载旧 chromedriver.exe,彻底删除手动 Service 配置,改用自动管理 |
报错 Unable to find a matching set of capabilities 或空白页无响应 |
启用了过时的反检测参数(如 excludeSwitches=["enable-automation"]) |
新版建议移除或改用更稳妥的绕过方式(如 options.add_experimental_option("useAutomationExtension", False)) |
控制台输出大量 DevTools listening on ... 但脚本卡住 |
Chrome 启动被安全软件拦截或端口冲突 | 添加 options.add_argument("--no-sandbox") 和 options.add_argument("--disable-dev-shm-usage")(仅限 Linux/CI 环境) |
? 进阶调试建议
启用详细日志有助于定位底层问题:
import logging
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
logging.basicConfig(level=logging.INFO)
options = Options()
options.set_capability("goog:loggingPrefs", {"browser": "ALL"}) # 启用浏览器日志
driver = webdriver.Chrome(options=options)
driver.get("https://www.google.com")
# 打印 JavaScript 控制台错误(如有)
for entry in driver.get_log("browser"):
if entry["level"] == "SEVERE":
print("⚠️ 浏览器错误日志:", entry)
driver.quit()
✅ 总结
-
不再需要手动下载和指定 chromedriver 路径 —— Selenium 4.10+ 的
selenium-manager已接管全部驱动生命周期; - 务必统一 Chrome 浏览器版本(推荐稳定版最新 Release),避免版本错位;
- 若必须保留自定义驱动路径(如离线环境),请确保
chromedriver --version输出与chrome://settings/help显示的 Chrome 主版本号严格一致(如 Chrome 128.x → 需 chromedriver 128.x); - 旧项目迁移时,优先删除
Service实例和executable_path参数,再逐步验证功能。
遵循以上方案,90% 以上的“升级后无法启动浏览器”问题可快速解决。











