Net::ReadTimeout Watir::Browser.new 时出错

Net::ReadTimeout Error when Watir::Browser.new

我正在尝试启动 watir 浏览器,

irb(main):001:0> require 'watir'
=> true
irb(main):002:0> browser = Watir::Browser.new

Watir 打开一个新的 chrome 浏览器,并在搜索栏中输入 data:,。等待 60 秒后,浏览器关闭,我在控制台中收到此错误:

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/lib/gems/2.3.0/gems/selenium-webdriver-3.14.0/lib/selenium/webdriver/remote/http/default.rb:121:in `response_for'
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.14.0/lib/selenium/webdriver/remote/http/default.rb:76:in `request'
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.14.0/lib/selenium/webdriver/remote/http/common.rb:62:in `call'
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.14.0/lib/selenium/webdriver/remote/bridge.rb:164:in `execute'
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.14.0/lib/selenium/webdriver/remote/bridge.rb:97:in `create_session'
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.14.0/lib/selenium/webdriver/remote/bridge.rb:53:in `handshake'
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.14.0/lib/selenium/webdriver/chrome/driver.rb:48:in `initialize'
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.14.0/lib/selenium/webdriver/common/driver.rb:44:in `new'
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.14.0/lib/selenium/webdriver/common/driver.rb:44:in `for'
    from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.14.0/lib/selenium/webdriver.rb:86:in `for'
    from /var/lib/gems/2.3.0/gems/watir-6.13.0/lib/watir/browser.rb:48:in `initialize'
    from (irb):3:in `new'
    from (irb):3
    from /usr/bin/irb:11:in `<main>'

我该如何解决这个问题?

正如您所说,当页面加载超过 60 秒时会发生这种情况,您可以通过以下代码增加页面加载超时

client = Selenium::WebDriver::Remote::Http::Default.new
client.read_timeout = 120 # seconds
driver = Selenium::WebDriver.for :firefox,http_client: client
b=Watir::Browser.new driver

现在您的代码将等待 120 秒以加载由 #click 引起的任何页面加载,并等待通过 goto 方法加载 url。

watir 6.19.0+

从 watir 版本 6.19.0 开始,他们提供了自己的 HTTPClient,因此您可以这样做:

Watir::Browser.new :firefox, http_client: { read_timeout: 120 }