WebDriverIO+Travis环境下ChromeDriver与Chrome兼容问题解决方案咨询
技术栈
- WebDriverIO(Web自动化测试)
- Travis(持续集成工具)
- 使用WebDriverIO专属的
selenium-standalone服务
Travis配置文件(travis.yml)
language: node_js node_js: - '18.16.0' dist: jammy addons: chrome: stable before_install: - # 启动Web应用并监听localhost - google-chrome-stable --headless --disable-gpu --remote-debugging-port=9222 http://localhost & install: - npm install script: - npm run generic - npm run login - npm run signup
报错信息
Error while creating session with the driver service. Stopping driver service: Could not start a new session. Response code 500. Message: session not created: This version of ChromeDriver only supports Chrome version 117
[0-0] Current browser version is 116.0.5845.187 with binary path /opt/google/chrome/chrome
提问:请问是否有办法彻底避免该问题再次发生?
要彻底解决Chrome与ChromeDriver版本不兼容的问题,推荐以下几种可靠方案:
方案1:改用WebDriverIO的chromedriver服务自动匹配版本
放弃selenium-standalone服务,改用WebDriverIO官方的chromedriver服务,它会自动检测系统中Chrome的版本,并下载对应版本的ChromeDriver,完全无需手动维护版本匹配。
操作步骤:
- 安装依赖:
npm install @wdio/chromedriver-service --save-dev
- 修改WebDriverIO配置文件(通常为
wdio.conf.js),替换服务并配置Chrome参数:
exports.config = { // ...其他原有配置 services: ['chromedriver'], capabilities: [{ browserName: 'chrome', 'goog:chromeOptions': { args: ['--headless', '--disable-gpu', '--remote-debugging-port=9222'] } }] };
- 移除Travis配置中
before_install里手动启动Chrome的命令,chromedriver服务会自动管理Chrome进程。
方案2:固定Chrome和ChromeDriver版本
若必须保留selenium-standalone服务,可在Travis中固定Chrome版本,同时指定selenium-standalone安装对应版本的ChromeDriver。
操作步骤:
- 修改Travis配置,指定具体Chrome版本:
addons: chrome: '116.0.5845.187' # 替换为需要固定的Chrome版本号
- 在WebDriverIO配置中指定
selenium-standalone的ChromeDriver版本:
exports.config = { services: [ ['selenium-standalone', { drivers: { chrome: { version: '116.0.5845.96', // 与Chrome 116匹配的ChromeDriver版本 arch: process.arch, baseURL: 'https://chromedriver.storage.googleapis.com' } } }] ] };
方案3:使用Travis的browser-tools插件自动管理版本
Travis提供的browser-tools插件可自动安装匹配的浏览器与驱动,避免版本不兼容:
before_install: - travis_retry npm install - travis_retry npx @travis-ci/browser-tools install chrome
该方式无需手动指定版本号,插件会自动确保Chrome与ChromeDriver版本匹配。
内容的提问来源于stack exchange,提问作者Ani

