Gem watir 报错 “`rbuf_fill': Net::ReadTimeout (Net::ReadTimeout)” 求助

imtinge · 2017年09月13日 · 最后由 imtinge 回复于 2017年09月13日 · 2254 次阅读

新学 watir 的小白,正常运行一段时间后,就会报错,不用 thread 也一样,请各位大神指导一下😂

代码如下:

def get_post_link_by_slice(page_slice)
  b = Watir::Browser.new :phantomjs

  page_slice.each do |page|
   b.goto page

   links = b.ul(class: "text-list").lis
   links.each do |li|
    link = li.a.href
    if link.end_with?('html') && link.include?('trsyw')
     puts "#{page} #{link}"
    end
   end
  end

  b.close
end

pages = get_pages()

threads = []
pages.each_slice(300) do |s|
  threads << Thread.new do
   get_post_link_by_slice(s)
  end
end

threads.each { |t| t.join }

报错如下:

/Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/protocol.rb:176:in `rbuf_fill': Net::ReadTimeout (Net::ReadTimeout)
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/protocol.rb:154:in `readuntil'
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/protocol.rb:164:in `readline'
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/http/response.rb:40:in `read_status_line'
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/http/response.rb:29:in `read_new'
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/http.rb:1446:in `block in transport_request'
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/http.rb:1443:in `catch'
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/http.rb:1443:in `transport_request'
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/http.rb:1416:in `request'
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/http.rb:1409:in `block in request'
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/http.rb:877:in `start'
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/2.4.0/net/http.rb:1407:in `request'
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/http/default.rb:124:in `response_for'
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/http/default.rb:78:in `request'
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/http/common.rb:61:in `call'
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/bridge.rb:170:in `execute'
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/oss/bridge.rb:581:in `execute'
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/oss/bridge.rb:52:in `get'
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/common/navigation.rb:32:in `to'
    from /Users/tanagisa/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/watir-6.8.4/lib/watir/browser.rb:82:in `goto'
    from test.rb:41:in `block in get_post_link_by_slice'
    from test.rb:40:in `each'
    from test.rb:40:in `get_post_link_by_slice'
    from test.rb:60:in `block in <main>'
    from test.rb:58:in `each_slice'
    from test.rb:58:in `with_index'
    from test.rb:58:in `<main>'

最近在用 watir 也遇到过这个错误,不过我也不知道是为啥。建议你在 get_post_link_by_slice 函数里加一个异常处理,把错误打印出来,看看是哪里出问题了。

zerzerheart 回复

错误就是“Net::ReadTimeout”,谢谢。我直接 rescue 了 retry

需要 登录 后方可回复, 如果你还没有账号请 注册新账号