【问题标题】:How do I catch this exception in Ruby?如何在 Ruby 中捕获此异常?
【发布时间】:2011-10-17 13:59:17
【问题描述】:

我是一名红宝石初学者,所以请耐心等待。

我正在使用 selenium-webdriver 和 rb-appscript gems 来做一些网页抓取。网站导航似乎是由 Net::Http 对象驱动的,它有一个 rbuf_fill 方法。

运行以下代码:

sites = File.open("sites.txt", "r") if File::exists?( "sites.txt" )

    if sites != nil
       while (line = sites.gets)

              driver.switch_to.default_content

          begin
                 driver.navigate.to line

          rescue Exception
                 line = line.split.join("\n")
                 puts line + " caused a timeout."
          end

       end

...

产生这个错误:

/opt/local/lib/ruby1.9/1.9.1/net/protocol.rb:140:in `rescue in rbuf_fill': Timeout::Error (Timeout::Error)
from /opt/local/lib/ruby1.9/1.9.1/net/protocol.rb:134:in `rbuf_fill'
from /opt/local/lib/ruby1.9/1.9.1/net/protocol.rb:116:in `readuntil'
from /opt/local/lib/ruby1.9/1.9.1/net/protocol.rb:126:in `readline'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:2219:in `read_status_line'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:2208:in `read_new'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1191:in `transport_request'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1177:in `request'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1170:in `block in request'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:627:in `start'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1168:in `request'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/remote/http/default.rb:73:in `response_for'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/remote/http/default.rb:41:in `request'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/remote/http/common.rb:34:in `call'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/remote/bridge.rb:406:in `raw_execute'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/remote/bridge.rb:384:in `execute'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/remote/bridge.rb:171:in `switchToDefaultContent'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/selenium-webdriver-2.2.0/lib/selenium/webdriver/common/target_locator.rb:68:in `default_content'
from auto.rb:25:in `<main>'

我不知道为什么我不能捕捉到这个异常。使用rescue Exception 应该捕获所有内容,但正如您所见,我的脚本仍然崩溃。

我还找到了说你必须明确捕捉超时的消息来源,所以我也尝试了:

rescue Timeout::Error

运气不好。

非常感谢您对此提供的任何帮助。

Ruby 版本:ruby 1.9.2p290(2011-07-09 修订版 32553)

操作系统:MacOS Snow Leopard 10.6.8 64 位

Selenium Webdriver 版本:2.2.0

【问题讨论】:

  • Ruby 通常缩进两个空格。

标签: ruby exception-handling selenium-webdriver net-http rb-appscript


【解决方案1】:

Ruby 标准库中的文件 'timeout.rb' 定义:

module Timeout
  # Raised by Timeout#timeout when the block times out.
  class Error < RuntimeError

所以你需要rescue 不是Timeout::Exception 而是Timeout::Error 或更一般的RuntimeError。那么它应该可以工作了。

【讨论】:

  • 抱歉,这实际上是一个错字,现在更正。我正在使用 Timeout::Error 并且仍然无法正常工作。刚刚也尝试了 RuntimeError ,它也没有抓住它。关于其他可能导致此功能不起作用的任何想法?
  • 哪一行是'auto.rb:25'行——是driver.navigate.to line
  • 哎呀,我今天工作一定很累。它实际上是 driver.switch_to.default_content 行。将该行移动到明确捕获 Timeout::Error 的开始/救援块中!
猜你喜欢
  • 2010-10-18
  • 1970-01-01
  • 2021-08-19
  • 2014-07-08
  • 1970-01-01
  • 2013-09-20
  • 2017-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多