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

后端 未结 4 1671
难免孤独
难免孤独 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:00

    I've been struggling with this one for more than a few hours ....

    It has been very frustrating to try to debug on Heroku CI. This ticket has helped me a lot to find a solution.

    I had this piece of code in the spec_helper.rb that was causing trouble (for if it helps someone):

    config.before(:each, type: :system, js: true) do
      driven_by :selenium_chrome_headless
    end
    

    It was bypassing all the capybara setup...

    0 讨论(0)
  • 2021-02-07 11:04

    I am quoting Ilya Vassilevsky from this post

    ChromeDriver is just a driver for Chrome. It needs the actual Chrome browser installed on the same machine to actually work.

    Heroku doesn't have Chrome installed on its dynos by default. You need to use a buildpack that installs Chrome. For example:

    https://github.com/dwayhs/heroku-buildpack-chrome

    You can see how it fetches Chrome:

    https://github.com/dwayhs/heroku-buildpack-chrome/blob/master/bin/compile#L36-38

    Then I read their discussion in the comments:

    Petr Gazarov says

    I tried this buildpack and it didn't work. I'm suspecting installing google chrome (or any browser) on heroku might be more involved.

    Ilya Vassilevsky replies

    Yes, Heroku is a very opinionated and closed platform. It should be much easier to set up Chrome with ChromeDriver on your own VM on AWS, Linode, or DigitalOcean.

    Petr Gazarov replies

    Thanks for your answer Ilya. I ended up re-writing with Watir with phantomjs because I couldn't get Heroku to install Chrome.

    You can read more info in that question. If something comes to my mind, I will post it.

    0 讨论(0)
  • 2021-02-07 11:04

    This worked for me. I replaced the line below:

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

    for this:

    Selenium::WebDriver::Chrome::Service.driver_path = "/app/.chromedriver/bin/chromedriver"
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题