Chrome 在 Rails 上 Ruby 的带有 Selenium 的 Heroku 上找不到二进制文件

Chrome binary not found on Heroku with Selenium for Ruby on Rails

两周前,我设法在 Heroku 上建立了一个工作环境,结合了 Capybara、Selenium、Chrome驱动程序和 Chrome 用于网络抓取。然而,一周前我一定改变了一些东西,这导致安装程序由于找不到 Chrome 二进制文件而崩溃。

WARN: Selenium::WebDriver::Error::UnknownError: unknown error: cannot find Chrome binary (Driver info: chromedriver=2.40.565383 (76257d1ab79276b2d53ee976b2c3e3b9f335cde7),platform=Linux 4.4.0-1019-aws x86_64)

我正在使用 Heroku-14 Stack 上的两个相关构建包

https://github.com/heroku/heroku-buildpack-xvfb-google-chrome
https://github.com/heroku/heroku-buildpack-chromedriver

使用的宝石:

gem 'selenium-webdriver','>=3.6.0'
gem 'chromedriver-helper'

我整个周末都在尝试通过将各种路径直接传递到 capybara.rb 初始化程序(并通过 运行ning heroku 运行 bash), 但无法正常工作。

capybara.rb

require "selenium/webdriver"
chrome_bin = ENV.fetch('GOOGLE_CHROME_SHIM', nil)
    chrome_opts = chrome_bin ? { "chromeOptions" => { "binary" => 'app/.apt/usr/bin/google-chrome-stable' } } : {}
    puts chrome_opts.to_s

    Capybara.register_driver :chrome do |app|
      Capybara::Selenium::Driver.new(
         app,
         browser: :chrome,
         desired_capabilities: Selenium::WebDriver::Remote::Capabilities.chrome(chrome_opts)
      )
    end

    Capybara.default_driver = :chrome
    Capybara.javascript_driver = :chrome

我还通过界面在 Heroku 中设置了 ENV 变量,但是当通过 heroku 运行 rails c 检查 ENV 时,BIN 变量似乎是从 buildpack 加载的,并且覆盖了我的配置。

I set GOOGLE_CHROME_BIN and GOOGLE_CHROME_SHIM to: /app/.apt/usr/bin/google-chrome

我不确定我必须做出什么样的改变才能让它重新工作。有很多拼图,我需要修复哪一个?欢迎提出建议!

已解决:

require "selenium/webdriver"

chrome_bin = ENV.fetch('GOOGLE_CHROME_SHIM', nil)

    Capybara.register_driver :chrome do |app|
      browser_options = ::Selenium::WebDriver::Chrome::Options.new
      browser_options.binary = chrome_bin
      Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)
    end

    Capybara.default_driver = :chrome
    Capybara.javascript_driver = :chrome

我猜你在过去几周升级到了最新的 selenium-webdriver 和 chromedriver。 chromeOptions 不再是要传递的有效密钥,您可以尝试将其更改为 goog:chromeOptions 但您实际上应该只使用 Selenium::WebDriver::Chrome::Options class[=14= 的实例]

Capybara.register_driver :chrome do |app|
  options = ::Selenium::WebDriver::Chrome::Options.new
  options.binary = ...
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)
end