Heroku:使用Watir / Selenium时无法连接到chromedriver 127.0.0.1:9515

它在本地运行(不指定driver_path ),但不在Heroku上运行。

码:

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

我在heroku rails控制台中确认了以下值

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

已安装的Buildpacks:

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

当前错误:

Selenium :: WebDriver :: Error :: WebDriverError:无法连接到chromedriver 127.0.0.1:9515

在SO上搜索unable to connect to chromedriver 127.0.0.1:9515返回一堆结果,但没有提到heroku。

也:

我考虑过phantomjs。 其他人在这里工作, 使用带有Heroku Rails Unicorn堆栈的无头浏览器

但它被弃用了。 在本地运行时出现以下错误。

不推荐使用PhantomJS的Selenium支持。 请改用无头Chrome / Firefox或HTMLUnit。

也试过:

为了透明,我也尝试了以下内容。

更改browser = Watir::Browser.new :chrome

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

虽然我没想到这会起作用。

还试过:

删除: https://github.com/heroku/heroku-buildpack-chromedriverhttps://github.com/heroku/heroku-buildpack-chromedriver

添加: https://github.com/heroku/heroku-buildpack-xvfb-google-chromehttps://github.com/heroku/heroku-buildpack-xvfb-google-chrome

添加headlessgem。

并运行watir gem页面中给出的以下脚本, http: watir

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

错误:

Selenium :: WebDriver :: Error :: UnknownError:未知错误:找不到Chrome二进制文件

我假设这失败了,因为我没有指定chrome binary / shim的位置。 在文档中使用headless时无法找到如何指定它。

试图建议:

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

在⬢app-name上运行/ usr / bin / chromedriver …运行.2151

(爱好)bash:/ usr / bin / chromedriver:没有这样的文件或目录

另请参阅以下在部署到heroku时提及chrome日志:

 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 

一些进展:

如果我进入heroku服务器,

heroku run bash --app app-name

并搜索名为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 

我可以在/app/.chromedriver/bin/chromedriver看到chromedriver二进制/app/.chromedriver/bin/chromedriver

所以我试过了

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

结果:

 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. 

但是然后运行heroku run rake selenium_namespace:task_one --app app-name给出相同的结果。

Selenium :: WebDriver :: Error :: WebDriverError:无法连接到chromedriver 127.0.0.1:9515 … /app/vendor/ruby-2.4.1/lib/ruby/2.4.0/net/http.rb:906 :在`connect in block in connect’中:无法打开到127.0.0.1:9515的TCP连接(连接被拒绝 – 连接(2)为“127.0.0.1”端口9515)(Errno :: ECONNREFUSED)…

这可以在Heroku上实现。


困惑的chromechromedriver

您的配置混合了chromedriverChromeGOOGLE_CHROME_SHIM指向Chrome可执行文件google-chrome-stable ,而不是chromedriver 。 下面的行导致Selenium执行错误的二进制文件,这会导致误导性的错误消息。

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

在撰写本文时(17年1月), chromedriver构建包自动将/app/.chromedriver/bin添加到$PATH变量。 如果删除上面的行,Selenium应该能够再次自动找到chromedriver

然后?

您可能已添加上述行以修复Selenium无法找到Chrome二进制文件。 该错误消息看起来像:

Selenium :: WebDriver :: Error :: UnknownError:未知错误:找不到Chrome二进制文件

你可以通过使用Selenium::WebDriver::Chrome::Options告诉Selenium Chrome二进制文件所在的位置来解决这个问题。 以下代码应该实现这一点。

 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

这应该可以使用标准的chromechromedriver构建包:

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

如果您在浏览器中自动执行点击,则可能需要使用heroku-buildpack-xvfb-google-chrome而不是vanilla chrome ,但这不仅仅是为了让无头镀铬运行。

我在这篇文章中引用了伊利亚·瓦西列夫斯基 的话

ChromeDriver只是Chrome的驱动程序。 它需要安装在同一台机器上的实际Chrome浏览器才能实际运行。

Heroku默认情况下没有在其dynos上安装Chrome。 您需要使用安装Chrome的buildpack。 例如:

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

您可以看到它如何获取Chrome:

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

然后我在评论中阅读他们的讨论:

彼得加扎罗夫

我尝试了这个buildpack,但它没有用。 我怀疑在heroku上安装谷歌浏览器(或任何浏览器)可能会涉及更多。

伊利亚·瓦西列夫斯基回答说

是的,Heroku是一个非常固执和封闭的平台。 在AWS,Linode或DigitalOcean上的自己的VM上使用ChromeDriver设置Chrome应该会容易得多。

Petr Gazarov回复道

谢谢你的回答Ilya。 我最后用Watr重写了phantomjs,因为我无法让Heroku安装Chrome。

您可以在该问题中阅读更多信息。 如果我想到了什么,我会发布它。

我已经在这个问题上苦苦挣扎了好几个小时….

尝试在Heroku CI上进行调试非常令人沮丧。 这张票帮我找了很多解决方案。

我在spec_helper.rb中有这段代码导致了麻烦(如果它对某人有帮助):

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

它绕过了所有的水豚设置……

是的,我同意Fabrizio。 必须使用DigitalOcean。 ChromeDriver需要Chrome浏览器,这会创建一个不太可塑的程序和更复杂的编码