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

tim*_*xyz 14 ruby-on-rails heroku watir selenium-chromedriver buildpack

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

码:

Selenium::WebDriver::Chrome.driver_path = ENV['GOOGLE_CHROME_SHIM']
browser = Watir::Browser.new :chrome
Run Code Online (Sandbox Code Playgroud)

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

ENV['GOOGLE_CHROME_BIN'] => "/app/.apt/opt/google/chrome/chrome"
ENV['GOOGLE_CHROME_SHIM'] => "/app/.apt/usr/bin/google-chrome-stable"
Run Code Online (Sandbox Code Playgroud)

已安装的Buildpacks:

https://github.com/heroku/heroku-buildpack-chromedriver
https://github.com/heroku/heroku-buildpack-google-chrome
Run Code Online (Sandbox Code Playgroud)

当前错误:

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

搜索unable to connect to chromedriver 127.0.0.1:9515SO返回一堆结果,但没有提到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-chromedriver

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

添加headless宝石.

并运行watirgem页面中给出的以下脚本,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
Run Code Online (Sandbox Code Playgroud)

错误:

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

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


试图建议:

heroku run /usr/bin/chromedriver --app app-name
Run Code Online (Sandbox Code Playgroud)

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

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

另请参阅以下chrome在部署到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
Run Code Online (Sandbox Code Playgroud)


一些进展:

如果我进入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
Run Code Online (Sandbox Code Playgroud)

我可以看到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.
Run Code Online (Sandbox Code Playgroud)

但随后运行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)...

lav*_*ick 4

这在 Heroku 上是可能的。


令人困惑chromechromedriver

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

Selenium::WebDriver::Chrome.driver_path = ENV['GOOGLE_CHROME_SHIM'] # WRONG!
Run Code Online (Sandbox Code Playgroud)

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

进而?

您可能添加了上面的行来修复 Selenium 无法找到 Chrome 二进制文件的问题。错误消息看起来像这样:

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

您可以通过使用 告诉 Selenium Chrome 二进制文件的位置来解决此问题Selenium::WebDriver::Chrome::Options。下面的代码应该可以完成这个任务。

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"
Run Code Online (Sandbox Code Playgroud)

构建包

这一切都可以通过标准包chromechromedriver构建包实现:

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

如果您要在浏览器中自动点击,则可能需要heroku-buildpack-xvfb-google-chrome而不是普通的chrome,但这不应该只是为了运行无头 Chrome 所必需的。