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

This is done locally (without driver_path ), but not on Heroku.

The code:

 Selenium::WebDriver::Chrome.driver_path = ENV['GOOGLE_CHROME_SHIM'] browser = Watir::Browser.new :chrome 

I confirmed below values ​​in geku rails console

 ENV['GOOGLE_CHROME_BIN'] => "/app/.apt/opt/google/chrome/chrome" ENV['GOOGLE_CHROME_SHIM'] => "/app/.apt/usr/bin/google-chrome-stable" 

Installed builds:

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

Current error:

Selenium :: WebDriver :: Error :: WebDriverError: unable to connect to chromedriver 127.0.0.1:9515

A search for unable to connect to chromedriver 127.0.0.1:9515 on SO returns a bunch of results, but no one mentions the hero.


Also:

I considered phantoms. Someone else has earned here, Using a mute browser with Heroku Rails Unicorn package

But it is out of date. Below errors when starting locally.

Selenium support for PhantomJS is deprecated. Use headless Chrome / Firefox or HTMLUnit.


Also tried:

For transparency, I also tried the following.

Change browser = Watir::Browser.new :chrome

In browser = Watir::Browser.new :chrome, headless: true

Although I did not expect this to work.


Also tried:

Removal: https://github.com/heroku/heroku-buildpack-chromedriver

Addendum: https://github.com/heroku/heroku-buildpack-xvfb-google-chrome .

Adding a headless gem.

And by running below the script indicated on the watir gem page, http://watir.com/guides/headless/ .

 require 'watir' require 'headless' headless = Headless.new headless.start b = Watir::Browser.start 'www.google.com' puts b.title b.close headless.destroy 

Mistake:

Selenium :: WebDriver :: Error :: UnknownError: Unknown error: Cannot find Chrome binary

I assume this failed because I did not specify the location of the chrome binary / gasket. Could not find how to indicate this when using headless in documents.


Tried on a suggestion:

 heroku run /usr/bin/chromedriver --app app-name 

Run / usr / bin / chromedriver on β¬’ app-name ... up, run.2151

(Hobby) bash: / usr / bin / chromedriver: No such file or directory

Also see below the logs that mention chrome when deployed to heroku:

 remote: -----> chromedriver app detected remote: -----> Looking up latest chromedriver version... remote: -----> Downloading chromedriver v2.33... remote: Archive: /tmp/chromedriver.zip remote: inflating: /tmp/build_cd35072c5b766edaa2b565cbff57e5d6/.chromedriver/bin/chromedriver remote: -----> Creating chromedriver export scripts... remote: -----> Google Chrome app detected ... remote: -----> Fetching https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb remote: -----> Installing google-chrome-stable_current_amd64.deb ... remote: -----> Creating google-chrome shims 


Progress:

If I ssh to heroku server,

heroku run bash --app app-name

Look for files called chrome ,

find /app/ -name "*chrome*"

 /app/.profile.d/chromedriver.sh /app/.profile.d/010_google-chrome.sh /app/.apt/etc/cron.daily/google-chrome /app/.apt/opt/google/chrome /app/.apt/opt/google/chrome/chrome /app/.apt/opt/google/chrome/chrome_100_percent.pak /app/.apt/opt/google/chrome/chrome-sandbox /app/.apt/opt/google/chrome/chrome_200_percent.pak /app/.apt/opt/google/chrome/google-chrome /app/.apt/opt/google/chrome/cron/google-chrome /app/.apt/usr/bin/google-chrome-stable /app/.apt/usr/bin/google-chrome /app/.apt/usr/share/menu/google-chrome.menu /app/.apt/usr/share/doc/google-chrome-stable /app/.apt/usr/share/applications/google-chrome.desktop /app/.apt/usr/share/gnome-control-center/default-apps/google-chrome.xml /app/.apt/usr/share/man/man1/google-chrome.1 /app/.apt/usr/share/appdata/google-chrome.appdata.xml /app/vendor/bundle/ruby/2.4.0/gems/selenium-webdriver-3.7.0/lib/selenium/webdriver/chrome /app/vendor/bundle/ruby/2.4.0/gems/selenium-webdriver-3.7.0/lib/selenium/webdriver/chrome.rb /app/vendor/bundle/ruby/2.4.0/gems/browser-2.4.0/test/unit/chrome_test.rb /app/vendor/bundle/ruby/2.4.0/gems/browser-2.4.0/lib/browser/platform/chrome_os.rb /app/vendor/bundle/ruby/2.4.0/gems/browser-2.4.0/lib/browser/chrome.rb /app/.chromedriver /app/.chromedriver/bin/chromedriver 

I see a binary file with chrome edges in /app/.chromedriver/bin/chromedriver .

So i tried

heroku run /app/.chromedriver/bin/chromedriver --app app-name

Result:

 Running /app/.chromedriver/bin/chromedriver on β¬’ app-name... up, run.2067 (Hobby) Starting ChromeDriver 2.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4) on port 9515 Only local connections are allowed. 

But then running heroku run rake selenium_namespace:task_one --app app-name gives the same result.

Selenium :: WebDriver :: Error :: WebDriverError: cannot connect to chromedriver 127.0.0.1:9515 ... / app / vendor / ruby ​​-2.4.1 / lib / ruby ​​/ 2.4.0 / net / http.rb: 906: in `rescue in block in connect ': Failed to open TCP connection from 127.0.0.1:9515 (Connection refused - connection (2) for port" 127.0.0.1 "9515) (Errno :: ECONNREFUSED) ...

+8
ruby-on-rails selenium-chromedriver heroku watir
source share
3 answers

It is possible on Heroku.


Obfuscating chrome and chromedriver

Your configuration mixes chrome and chrome . GOOGLE_CHROME_SHIM points to Chrome being run by google-chrome-stable , not chromedriver . The line below causes Selenium to execute incorrect binary code, which leads to an erroneous error message.

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

Since writing this (January '17), the chromedriver compilation chromedriver automatically add /app/.chromedriver/bin to the $PATH variable. If you delete the above line, Selenium will again be able to automatically find the chromedriver .

And then?

You probably added the line above to fix Selenium without being able to find the Chrome binary. The error message for this would look something like this:

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 do 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 # 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 be possible with the standard chrome and chromedriver build packages:

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 are automating clicks in the browser, but this should not be required just to run without a chronometer.

0
source share

I quote Ilya Vasilievsky from this article

ChromeDriver is just a driver for Chrome. To actually use the actual Chrome browser installed on the same computer, you need it.

Heroku is not installed by default on its Chrome speakers. You need to use the buildpack that installs Chrome. For example:

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

You can see how it retrieves 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 did not work. I suspect that installing google chrome (or any browser) on heroku might be more complicated.

Ilya Vasilievsky is responsible

Yes, Heroku is a very stubborn and closed platform. It's much easier to configure Chrome using ChromeDriver on your own VM on AWS, Linode, or DigitalOcean.

Petr Gazarov is responsible

Thank you for your reply, Ilya. I ended up rewriting with Watir with phantomjs because I couldn't get Heroku to install Chrome.

You can read more information in this matter. If something comes to my mind, I will send it.

0
source share

Yes, I agree with Fabrizio. Must use DigitalOcean. ChromeDriver requires the Chrome browser, which creates a less malleable program and more complex coding.

-2
source share

All Articles