4

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

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Not just for solving the problem but also for allowing me to stop working now and to spend my evening with my partner and little boy. You can't imagine how happy I am. I'll also post my working code above in case someone else has this issue.
0

I know this comes in a bit late. But I had the same issue, and just figured it out. I was going NUTS. Turns out Heroku doesn't really maintain their buildpacks. Therefore, install these buildpacks:

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

and of course your heroku/ruby buildpack after those two.

Boom. It'll work like clockwork. Additionally super important, don't forget to explicitly quit the browser after each request, or after a certain amount of time, otherwise it'll be spawned indefinitely, will use up all of your memory, and eventually kill the server. You can do that with

session.quit

I guess for you (wherever you're calling a new capybara session.. maybe inyour controller?) it should look something like this:

 session = Capybara::Session.new(:selenium)
   // your browser actions or logic here

 session.quit

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.