问题
I'm trying to run headless chrome on Heroku with ruby. I've installed the buildpacks https://github.com/heroku/heroku-buildpack-google-chrome/ and https://github.com/heroku/heroku-buildpack-chromedriver and have set the Selenium driver_path to the correct location (I've checked this as before setting the Selenium driver path I get cannot find Chrome binary
, after setting it to the GOOGLE_CHROME_BIN
variable set by the buildpack I get the unable to connect to chromedriver
).
When I try to start Selenium / Watir with Watir::Browser.new :chrome
or Watir::Browser.new :chrome, headless:true
I get Selenium::WebDriver::Error::WebDriverError: unable to connect to chromedriver 127.0.0.1:9516
Any suggestions are appreciated
回答1:
We also had the same problem due to the combination of configuration errors and passing the correct options hash to Watir::Browser.new
. We are using watir (6.10.3)
, selenium-webdriver (~> 3.4, >= 3.4.1)
and ruby '2.4.0'
.
To get everything working on Heroku we added the following buildpacks:
- https://github.com/heroku/heroku-buildpack-google-chrome
- https://github.com/heroku/heroku-buildpack-chromedriver
- heroku/ruby
To successfully initialize the browser you need to pass the path to chromedriver bin within the options hash. heroku-buildpack-chromedriver provides an environment variable GOOGLE_CHROME_SHIM
for the bin search path. At the end the code which will work locally and on Heroku looks like this:
opts = {
headless: true
}
if (chrome_bin = ENV.fetch('GOOGLE_CHROME_SHIM', nil))
opts.merge!( options: {binary: chrome_bin})
end
browser = Watir::Browser.new :chrome, opts
browser.goto "your url"
回答2:
If anyone else stumbles across this, there's a good example here: https://github.com/jormon/minimal-chrome-on-heroku
来源:https://stackoverflow.com/questions/45951289/ruby-heroku-seleniumwebdrivererrorwebdrivererror-unable-to-connect-to-c