开发者

How do I catch this exception in Ruby?

I'm a ruby beginner so bear with me.

I am using the selenium-webdriver and rb-appscript gems to do some webscraping. The navigation to websites seems to be driven by the Net::Http object, which has a rbuf_fill method.

Running the following code:

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

    if sites != nil
       while (li开发者_高级运维ne = 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

...

Produces this error:

/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>'

I have no idea why I can't catch this exception. Using rescue Exception should catch everything, but as you can see my script still crashes.

I have also found sources that say you must catch the timeout explicitly so I also tried:

rescue Timeout::Error

without any luck.

Any help is greatly appreciated on this one.

Ruby version: ruby 1.9.2p290 (2011-07-09 revision 32553)

OS: MacOS Snow Leopard 10.6.8 64-bit

Selenium Webdriver version: 2.2.0


The file 'timeout.rb' in Ruby's standard library defines:

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

So what you need to rescue is not Timeout::Exception but rather Timeout::Error or more generically RuntimeError. Then it should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜