Capybara,Selenium,无头Chrome,Ubuntu Net :: ReadTimeout

时间:2017-09-11 20:17:53

标签: google-chrome selenium ubuntu capybara selenium-chromedriver

我看到了一个与我非常相似的问题。在OS X上一切正常,但在ubuntu 14.04上抛出错误。回溯那篇文章,我收到同样的错误。原来的海报放弃了并使用了Poltergeist / PhantomJS。我现在已经不再使用PhantomJS来使用Chrome了。

Net::ReadTimeout: Net::ReadTimeout and Selenium::WebDriver::Error::UnknownError: unknown error: Chrome failed to start on Rails 5.1.beta System Test

以下是我在Ubuntu上的安装步骤:

apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 40976EAF437D05B5
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - 
cat <<EOF > /etc/apt/sources.list.d/google-chrome.list
deb http://dl.google.com/linux/chrome/deb/ stable main
EOF
apt-get update
apt-get install --force-yes -y google-chrome-stable
cd /root/ && curl -O "http://chromedriver.storage.googleapis.com/2.32/chromedriver_linux64.zip"
cd /root/ && unzip chromedriver_linux64.zip && cp chromedriver /usr/bin

一步一步,我确认谷歌铬,chromedriver安装正确,工作正常。 Selenium驱动也行得很好。

link = ENV['LINK'] || "https://www.amazon.com"
#https://stackoverflow.com/questions/44424200/how-do-i-use-selenium-webdriver-on-headless-chrome
Selenium::WebDriver::Chrome.driver_path="/usr/bin/chromedriver" if RUBY_PLATFORM.include? "linux"
options = %w[--headless --disable-gpu]
options +=  %w[--binary='/usr/bin/google-chrome'] if RUBY_PLATFORM.include? "linux"
driver = Selenium::WebDriver.for :chrome, switches: options
driver.navigate.to "#{link}"
driver.save_screenshot("./screen.png")
driver.quit

Capybara测试在访问网址时超时。

require 'capybara'
include Capybara::DSL

link = ENV['LINK'] || "https://www.amazon.com"
options = %w[--headless --disable-gpu]
options +=  %w[--binary='/usr/bin/google-chrome'] if RUBY_PLATFORM.include? "linux"
Selenium::WebDriver::Chrome.driver_path="/usr/bin/chromedriver" if RUBY_PLATFORM.include? "linux"
Capybara.register_driver(:headless_chrome) do |app|
    capabilities = Selenium::WebDriver::Remote::Capabilities.chrome( chromeOptions: { args: options } )
    Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: capabilities )
end
Capybara.javascript_driver = :headless_chrome
session = Capybara::Session.new(:headless_chrome)
session.visit "#{link}"

错误是以下消息:

session.visit "#{link}"

Net::ReadTimeout: Net::ReadTimeout
    from /usr/lib/ruby/2.3.0/net/protocol.rb:158:in `rbuf_fill'
    from /usr/lib/ruby/2.3.0/net/protocol.rb:136:in `readuntil'
    from /usr/lib/ruby/2.3.0/net/protocol.rb:146:in `readline'
    from /usr/lib/ruby/2.3.0/net/http/response.rb:40:in `read_status_line'
    from /usr/lib/ruby/2.3.0/net/http/response.rb:29:in `read_new'
    from /usr/lib/ruby/2.3.0/net/http.rb:1437:in `block in transport_request'
    from /usr/lib/ruby/2.3.0/net/http.rb:1434:in `catch'
    from /usr/lib/ruby/2.3.0/net/http.rb:1434:in `transport_request'
    from /usr/lib/ruby/2.3.0/net/http.rb:1407:in `request'
    from /usr/lib/ruby/2.3.0/net/http.rb:1400:in `block in request'
    from /usr/lib/ruby/2.3.0/net/http.rb:853:in `start'
    from /usr/lib/ruby/2.3.0/net/http.rb:1398:in `request'
    from /var/www/railsapp/vendor/bundle/ruby/2.3.0/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/http/default.rb:107:in `response_for'
    from /var/www/railsapp/vendor/bundle/ruby/2.3.0/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/http/default.rb:58:in `request'
    from /var/www/railsapp/vendor/bundle/ruby/2.3.0/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/http/common.rb:59:in `call'
    from /var/www/railsapp/vendor/bundle/ruby/2.3.0/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/bridge.rb:649:in `raw_execute'
... 10 levels...
    from /usr/bin/irb:11:in `<top (required)>'
    from /usr/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/cli/exec.rb:74:in `load'
    from /usr/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/cli/exec.rb:74:in `kernel_load'
    from /usr/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/cli/exec.rb:27:in `run'
    from /usr/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/cli.rb:332:in `exec'
    from /usr/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
    from /usr/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
    from /usr/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch'
    from /usr/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/cli.rb:20:in `dispatch'
    from /usr/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/vendor/thor/lib/thor/base.rb:440:in `start'
    from /usr/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/cli.rb:11:in `start'
    from /usr/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/exe/bundle:34:in `block in <top (required)>'
    from /usr/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/friendly_errors.rb:100:in `with_friendly_errors'
    from /usr/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/exe/bundle:26:in `<top (required)>'
    from /usr/bin/bundle:23:in `load'
    from /usr/bin/bundle:23:in `<main>'

1 个答案:

答案 0 :(得分:2)

无头Chrome和支持它的chromedriver相对较新。将您的selenium-webdriver更新到最新版本(本答案为3.5.2)以获得支持。一旦你完成了,如果你正在使用最新的Capybara,你也可以尝试使用Capybara提供的注册驱动程序,

if RUBY_PLATFORM.include? "linux"
  Selenium::WebDriver::Chrome.driver_path = "/usr/bin/chromedriver"
  Selenium::WebDriver::Chrome.path = "/usr/bin/google-chrome"
end
Capybara.javascript_driver = :selenium_chrome_headless

而不是需要注册自己的驱动程序。