Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku: Unable to find chromedriver when using Selenium

I have a Ruby code that does this:

browser = Watir::Browser.new(:chrome, switches: switches, headless: true)
browser.goto(....)

When I run the code on Heroku I get

Selenium::WebDriver::Error::WebDriverError:  Unable to find chromedriver. Please download the server from http://chromedriver.storage.googleapis.com/index.html and place it somewhere on your PATH. More info at https://github
.com/SeleniumHQ/selenium/wiki/ChromeDriver.

I've seen posts like Heroku: unable to connect to chromedriver 127.0.0.1:9515 when using Watir/Selenium but I don't know how to properly configure the buildpacks. I've tried with:

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

but then when I try to push the changes to Heroku I get:

Error Plugin: chromedriver: files attribute must be specified in /Users/leticia/.local/share/heroku/node_modules/chromedriver/package.json

Can someone give me a step by step on how to set the necessary buildpacks to make the Watir gem work in Heroku?

Thank you

Update:

I've required 'webdrivers' and now I'm getting Selenium::WebDriver::Error::WebDriverError: unable to connect to chromedriver 127.0.0.1:9515 .

I've tried configuring the env vars to:

ENV['GOOGLE_CHROME_BIN'] = "/app/.apt/opt/google/chrome/chrome"
ENV['GOOGLE_CHROME_SHIM'] = "/app/.apt/usr/bin/google-chrome-stable"

and doing this:

options = Selenium::WebDriver::Chrome::Options.new
chrome_bin_path = ENV.fetch('GOOGLE_CHROME_SHIM', nil)
options.binary = chrome_bin_path if chrome_bin_path 
options.add_argument('--headless')
driver = Selenium::WebDriver.for :chrome, options: options

but I'm still getting the error in the last line.

Update 2:

I moved to Dokku instead of Heroku and I get the same error. Selenium::WebDriver::Error::WebDriverError: unable to connect to chromedriver 127.0.0.1:9515.

like image 358
Leticia Esperon Avatar asked Jun 04 '18 21:06

Leticia Esperon


People also ask

Do we need to install ChromeDriver for Selenium?

As Google Chrome dominates the browser market, the use of a ChromeDriver becomes a must. Selenium WebDriver uses the ChromeDriver to communicate test scripts with Google Chrome. It is used to navigate between web pages and provide input to the same.


1 Answers

The way to do it is:

  1. Add the buildpacks with

    heroku buildpacks:add https://github.com/heroku/heroku-buildpack-google-chrome
    heroku buildpacks:add https://github.com/heroku/heroku-buildpack-chromedriver
    
  2. Add the env vars GOOGLE_CHROME_BIN and GOOGLE_CHROME_SHIM in Heroku both with value /app/.apt/opt/google/chrome/chrome i.e.

    heroku config:set GOOGLE_CHROME_BIN=/app/.apt/opt/google/chrome/chrome
    heroku config:set GOOGLE_CHROME_SHIM=/app/.apt/opt/google/chrome/chrome
    
  3. Use watir in the following way

    args = %w[--disable-infobars --headless window-size=1600,1200 --no-sandbox --disable-gpu]
    options = {
           binary: ENV['GOOGLE_CHROME_BIN'],
           prefs: { password_manager_enable: false, credentials_enable_service: false },
           args:  args
         }    
    @browser = Watir::Browser.new(:chrome, options: options)
    
like image 197
Gabriel Avatar answered Oct 11 '22 12:10

Gabriel