Heroku:使用Watir / Selenium时无法连接到chromedriver 127.0.0.1:9515 [英] Heroku: unable to connect to chromedriver 127.0.0.1:9515 when using Watir/Selenium
问题描述
这在本地运行(不指定 driver_path
),但不在Heroku上运行。
This runs locally (without specifying driver_path
), but not on Heroku.
代码:
Selenium::WebDriver::Chrome.driver_path = ENV['GOOGLE_CHROME_SHIM']
browser = Watir::Browser.new :chrome
我已经在heroku rails控制台中确认了以下值
I've confirmed below values in heroku rails console
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
Selenium::WebDriver::Error::WebDriverError: unable to connect to chromedriver 127.0.0.1:9515
搜索无法连接到chromedriver 127.0.0.1:9515
SO返回一堆结果,但没有提及heroku。
Searching unable to connect to chromedriver 127.0.0.1:9515
on SO returns a bunch of results but none mention heroku.
我考虑了phantomjs。有人在这里工作,
使用无头具有Heroku Rails Unicorn堆栈的浏览器
I considered phantomjs. Someone else got it working here, Using a headless browser with Heroku Rails Unicorn stack
但已弃用。在本地运行时出现以下错误。
But its deprecated. Below error when running it locally.
对PhantomJS的硒支持已被弃用。改用无头
Chrome / Firefox或HTMLUnit。
Selenium support for PhantomJS is deprecated. Use headless Chrome/Firefox or HTMLUnit instead.
为了透明起见,我还尝试了以下方法。
For transparency, I also tried the following.
更改浏览器= Watir :: Browser.new:chrome
到浏览器= Watir :: Browser.new :chrome,headless:true
尽管我没想到这会起作用。
Though I didn't expect this to work.
删除:
https://github.com/heroku/heroku-buildpack-chromedriver
添加:
https://github.com/heroku/heroku-buildpack-xvfb-google-chrome
。
添加
并运行 watir
gem页面中给出的以下脚本, http://watir.com/guides/headless/ 。
And running the below script given in 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
错误:
Selenium :: WebDriver :: Error :: UnknownError:未知错误:找不到
Chrome二进制文件
Selenium::WebDriver::Error::UnknownError: unknown error: cannot find Chrome binary
我认为这失败了,因为我没有指定chrome二进制文件/ shim的位置。在文档中使用 headless
时,找不到指定方法。
I assume this failed because I didn't specify location of the chrome binary/shim. Couldn't find how to specify this when using headless
in the docs.
heroku run /usr/bin/chromedriver --app app-name
在⬢app-上运行/ usr / bin / chromedriver 2151
Running /usr/bin/chromedriver on ⬢ app-name... up, run.2151
(爱好)bash:/ usr / bin / chromedriver:没有这样的文件或目录
(Hobby) bash: /usr/bin/chromedriver: No such file or directory
另请参阅以下在部署到heroku时提及 chrome
的日志:
Also see below logs that mention chrome
when deploying 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
如果我进入heroku服务器,
If I ssh into the heroku server,
heroku运行bash --app应用程序名称
并搜索名称为 chrome
,
查找/ app /-名称 * 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二进制文件。
I can see chromedriver binary at /app/.chromedriver/bin/chromedriver
.
所以我尝试了
heroku run /app/.chromedriver/bin / chromedriver --app app-nam e
结果:
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运行rake selenium_namespace:task_one --app app-name
给出相同的结果。
But then running heroku run rake selenium_namespace:task_one --app app-name
gives the same result.
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:在$ b $中营救b块在connect':无法打开与127.0.0.1:9515
的TCP连接(拒绝连接- 127.0.0.1端口9515的connect(2))
(Errno :: ECONNREFUSED).. 。
Selenium::WebDriver::Error::WebDriverError: unable to 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 to 127.0.0.1:9515 (Connection refused - connect(2) for "127.0.0.1" port 9515) (Errno::ECONNREFUSED) ...
推荐答案
在Heroku上可能是这样。
您的配置混合了 chromedriver 和 Chrome 。 GOOGLE_CHROME_SHIM
指向 Chrome 可执行文件 google-chrome-stable
,而不是 chromedriver 。下面的行导致Selenium执行错误的二进制文件,从而导致产生误导性的错误消息。
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!
撰写本文时(18年1月), chromedriver
构建包会自动将 /app/.chromedriver/bin
添加到 $ PATH
变量中。如果您删除,则上述Selenium行应该可以再次自动找到 chromedriver
。
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.
您可能添加了以上代码,以修复Selenium无法找到Chrome二进制文件的问题。相应的错误消息应类似于:
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:未知错误:找不到
Chrome二进制文件
Selenium::WebDriver::Error::UnknownError: unknown error: cannot find Chrome binary
您可以使用 Selenium告诉Selenium Chrome二进制文件在哪里,从而解决此问题: :WebDriver :: Chrome :: Options
。以下代码应该可以实现这一点。
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
使用标准 chrome
和 chromedriver
构建包:
https://github.com/heroku/ heroku-buildpack-google-chrome.git
https://github.com/heroku/heroku-buildpack-chromedriver.git
您可能需要 heroku-buildpack-xvfb-google-chrome
而不是香草 chrome
如果要在浏览器中自动执行点击,则 st使无头的chrome运行。
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.
这篇关于Heroku:使用Watir / Selenium时无法连接到chromedriver 127.0.0.1:9515的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!