3

我使用 watir-webdriver 来自动化一些日常任务,最近我完美的工作脚本在开始时中止了。当我使用 browser.goto "webpage" 时,网页没有完全加载,因为页面底部有一个计数器,并且计数器服务似乎暂时关闭。有时它会加载,有时不会。否则,我需要的所有 html 元素都已加载且可见,但页面无法完全加载,因此 30 秒后我总是收到 tiemout 错误,并且我的 Ruby 脚本以以下错误消息终止:

/opt/ruby1.8/lib/ruby/1.8/timeout.rb:64:in `rbuf_fill': execution expired (Timeout::Error)
    from /opt/ruby1.8/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill'
    from /opt/ruby1.8/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
    from /opt/ruby1.8/lib/ruby/1.8/net/protocol.rb:126:in `readline'
    from /opt/ruby1.8/lib/ruby/1.8/net/http.rb:2028:in `read_status_line'
    from /opt/ruby1.8/lib/ruby/1.8/net/http.rb:2017:in `read_new'
    from /opt/ruby1.8/lib/ruby/1.8/net/http.rb:1051:in `request'
    from /opt/ruby1.8/lib/ruby/1.8/net/http.rb:1037:in `request'
    from /opt/ruby1.8/lib/ruby/1.8/net/http.rb:543:in `start'
    from /opt/ruby1.8/lib/ruby/1.8/net/http.rb:1035:in `request'
    from /opt/ruby1.8/lib/ruby/gems/1.8/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/http/default.rb:82:in `response_for'
    from /opt/ruby1.8/lib/ruby/gems/1.8/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/http/default.rb:38:in `request'
    from /opt/ruby1.8/lib/ruby/gems/1.8/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
    from /opt/ruby1.8/lib/ruby/gems/1.8/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/bridge.rb:598:in `raw_execute'
    from /opt/ruby1.8/lib/ruby/gems/1.8/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/bridge.rb:576:in `execute'
    from /opt/ruby1.8/lib/ruby/gems/1.8/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/bridge.rb:99:in `get'
    from /opt/ruby1.8/lib/ruby/gems/1.8/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/common/navigation.rb:14:in `to'
    from /opt/ruby1.8/lib/ruby/gems/1.8/gems/watir-webdriver-0.6.1/lib/watir-webdriver/browser.rb:63:in `goto'

有什么方法可以避免程序终止并使用未完全加载的页面?我的意思是如果可能的话,任何超时异常处理,最重要的是我的 Ruby 脚本继续没有终止。我试过这个,但没有奏效,因为我只是 3 天的 ruby​​ 程序员:

begin
  browser.goto "http://example.com"
rescue Watir::Exception::NavigationException => e #catch the Exception
  puts "Page did not load: #{e}" 
  # program continues from here 
end
4

1 回答 1

1

尝试救援Timeout::Error而不是Watir::Exception::NavigationException.

于 2012-08-20T19:38:00.917 回答