rubygoogle-chromeseleniumherokucapybara

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


Two weeks ago, I managed to have a working environment on Heroku, combining Capybara, Selenium, Chromedriver and Chrome for web scraping. However, one week ago I must have changed something, which causes the setup to crash due to the Chrome binary not being found.

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)

I am using the two relevant buildpacks on the Heroku-14 Stack

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

Used gems:

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

I've spent the weekend trying to get this to work by passing various paths directly into the capybara.rb initializer (and compared these by running heroku run bash), but could not get it working.

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

I have also set the ENV vars in Heroku via the interface but when checking with ENV via heroku run rails c, it seems that the BIN var is loaded from the buildpack, and overrides my configuration.

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

I'm no sure what kind of changes I have to make to get it working again. There are quite a few puzzle pieces, which one do I need to fix? Suggestions welcome!

SOLVED:

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

Solution

  • I'm guessing you upgraded to the latest selenium-webdriver and chromedriver in the last few weeks. chromeOptions is no longer a valid key to pass, you can try changing it to goog:chromeOptions but you really should just be using an instance of the Selenium::WebDriver::Chrome::Options class

    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