selenium.common.exceptions.SessionNotCreatedException:消息:会话未创建:此版本的 ChromeDriver 仅支持 Chrome 版本 120 当前浏览器版本为 124.0.6367.78
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 120 Current browser version is 124.0.6367.78
ID: python/pytest-selenium-webdriver-not-found
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 4.18 | active | — | — | — |
根因分析
ChromeDriver 二进制版本与已安装的 Chrome 浏览器版本不匹配。Selenium 4.x 通过 Selenium Manager 自动管理驱动,但 PATH 上过时的 chromedriver 会优先使用并导致不匹配。
English
ChromeDriver binary version does not match the installed Chrome browser version. Selenium 4.x auto-manages drivers via Selenium Manager, but a stale chromedriver on PATH takes precedence and causes the mismatch.
解决方案
-
95% 成功率
which chromedriver && sudo rm $(which chromedriver) pytest tests/e2e/ -v # Selenium Manager downloads the correct driver on first run
-
90% 成功率
# Dockerfile RUN apt-get install -y google-chrome-stable=124.* # or use selenium/standalone-chrome:124.0 image # pytest with RemoteWebDriver pointing to the container
无效尝试
常见但无效的做法:
-
85% 失败
Security risk and often blocked by corporate IT; also brittle since Chrome auto-updates.
-
90% 失败
Headless still launches a real Chrome binary; the version check happens before mode selection.