You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails/Capybara问题:ChromeDriver提示缺少Chrome二进制文件但实际存在

解决Capybara测试中ChromeDriver提示找不到Chrome二进制文件的问题

问题现象

近期执行bundle update或Chrome/Chromedriver更新后,Capybara功能测试突然失败,ChromeDriver报错找不到Chrome二进制文件,但终端执行/usr/bin/google-chrome可正常启动浏览器。报错信息如下:

Selenium::WebDriver::Error::UnknownError:
       unknown error: no chrome binary at /usr/bin/google-chrome
     # #0 0x561b988f1e43 <unknown>
     # #1 0x561b985ba687 <unknown>
     ...

原配置文件已正常运行多年,内容如下:

require "capybara/rails"
require "capybara/rspec"
require "selenium/webdriver"

Capybara.register_driver :headless_chrome do |app|
  options = Selenium::WebDriver::Chrome::Options.new
  options.add_argument("--headless=true")
  options.add_argument("--ignore-certificate-errors")
  options.add_argument("--window-size=1280,1920")
  options.add_argument("--proxy-server=#{Billy.proxy.host}:#{Billy.proxy.port}")

  Capybara::Selenium::Driver.new(app, browser: :chrome, options:)
end

Capybara.javascript_driver = :headless_chrome

解决方案

  • 检查Chrome与Chromedriver版本兼容性
    执行chromedriver --version和google-chrome --version确认两者大版本是否一致,新版本Chrome要求Chromedriver版本完全匹配,版本不兼容会导致路径识别异常。

  • 显式指定Chrome稳定版二进制路径
    部分系统中Chrome的实际可执行文件是google-chrome-stable,即使存在google-chrome软链,新版本Selenium可能无法正确识别。修改配置文件,添加二进制路径指定:

    Capybara.register_driver :headless_chrome do |app|
      options = Selenium::WebDriver::Chrome::Options.new
      options.binary = "/usr/bin/google-chrome-stable" # 添加这行
      options.add_argument("--headless=true")
      options.add_argument("--ignore-certificate-errors")
      options.add_argument("--window-size=1280,1920")
      options.add_argument("--proxy-server=#{Billy.proxy.host}:#{Billy.proxy.port}")
    
      Capybara::Selenium::Driver.new(app, browser: :chrome, options:)
    end
    
  • 验证测试环境的PATH与文件权限
    测试进程的环境变量PATH可能与终端不一致,可在测试代码中临时打印puts ENV['PATH']确认;同时确保测试运行用户对/usr/bin/google-chrome有执行权限,执行chmod +x /usr/bin/google-chrome修复权限问题。

  • 适配新版本Chrome更新依赖版本
    若使用的Selenium或Capybara版本过旧,无法适配新版Chrome,需确保selenium-webdriver版本在4.x以上,可在Gemfile中指定版本后重新执行bundle install。

内容的提问来源于stack exchange,提问作者Rob Lucas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 10:16:32