问题
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
.
回答1:
The way to do it is:
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
Add the env vars
GOOGLE_CHROME_BIN
andGOOGLE_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
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)
回答2:
I can't assure this is sufficient for everything you need, but if you add webdrivers
to your gemfile and require it, it will automatically download chromedriver for you.
来源:https://stackoverflow.com/questions/50688828/heroku-unable-to-find-chromedriver-when-using-selenium