Heroku: unable to connect to chromedriver 127.0.0.1:9515 when using Watir/Selenium

后端 未结 4 1676
难免孤独
难免孤独 2021-02-07 10:24

This runs locally (without specifying driver_path), but not on Heroku.

Code:

Selenium::WebDriver::Chrome.driver_path = ENV[         


        
4条回答
  •  攒了一身酷
    2021-02-07 11:17

    This is possible on Heroku.


    Confusing chrome and chromedriver

    Your configuration is mixing up chromedriver and Chrome. GOOGLE_CHROME_SHIM points to the Chrome executable google-chrome-stable, not to chromedriver. The line below results in Selenium executing the wrong binary, which results in the misleading error message.

    Selenium::WebDriver::Chrome.driver_path = ENV['GOOGLE_CHROME_SHIM'] # WRONG!
    

    As of writing this (Jan '18) the chromedriver build pack automatically adds /app/.chromedriver/bin to the $PATH variable. If you delete the above line Selenium should again be able to find chromedriver automatically.

    And then?

    You probably added the line above to fix Selenium not being able to find the Chrome binary. The error message for that would have looked something like:

    Selenium::WebDriver::Error::UnknownError: unknown error: cannot find Chrome binary

    You can fix this by telling Selenium where the Chrome binary is located using Selenium::WebDriver::Chrome::Options. The following code should accomplish that.

    options = Selenium::WebDriver::Chrome::Options.new
    chrome_bin_path = ENV.fetch('GOOGLE_CHROME_SHIM', nil)
    options.binary = chrome_bin_path if chrome_bin_path # only use custom path on heroku
    options.add_argument('--headless') # this may be optional
    driver = Selenium::WebDriver.for :chrome, options: options
    driver.navigate.to "https://stackoverflow.com"
    

    Buildpacks

    This should all be possible with the standard chrome and chromedriver build packs:

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

    You may need heroku-buildpack-xvfb-google-chrome instead of vanilla chrome if you're automating clicks in the browser, but that should not be required just to get headless chrome running.

提交回复
热议问题