Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

ruby on rails - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...