Watir / Selenium – browser.goto在Chrome和Firefox上不断出现TimeOut错误

Watir webdriver遇到了一个非常烦人的问题..

我调试了一下,发现我总是在一个简单的@ browser.goto行上得到TimeOut :: Error,即使我可以直观地看到页面已经完全加载了…

场景是这样的:打开浏览器,转到url,点击几个链接,然后突然一点,脚本停止继续浏览,等待大约30秒以上并抛出错误。 尝试了Chrome和FF:Chrome更糟糕,通常第二或第三个链接点击会触发; 对于FF,有时需要10多页浏览…

打赌存在一些环境或可比性问题:

jd@deskbox:~$ uname -am Linux deskbox 3.0.0-12-generic #20-Ubuntu SMP Fri Oct 7 14:56:25 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux jd@deskbox:~$ ruby -v ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux] jd@deskbox:~$ rails -v Rails 3.2.1 jd@deskbox:~$ gem -v 1.8.15 jd@deskbox:~$ gem list|grep webdriver selenium-webdriver (2.12.0) watir-webdriver (0.5.3) 

有人可以帮忙吗? 源代码在这里:

 #!/usr/bin/env ruby require 'watir-webdriver' require 'pry' class Search attr_accessor :browser, :company_url, :company_name def initialize() @browser = Watir::Browser.start 'http://www.google.com', :chrome end def visit_company_home_via_google(company) @company_name = company @browser.goto "http://www.google.com/search?q=#{company}" link = @browser.div(:id=>'ires').link return nil unless link.exists? @browser.goto link.href @company_url ||= @browser.url @company_url end def logoff() @browser.close if @browser end end s = Search.new puts s.visit_company_home_via_google("github") puts s.visit_company_home_via_google("Mashable") puts s.visit_company_home_via_google("Barracuda Networks") s.logoff 

我的结果是:

 jd@deskbox:~/cuda$ ./search.rb https://github.com/ /usr/local/lib/ruby/1.9.1/net/protocol.rb:140:in `rescue in rbuf_fill': Timeout::Error (Timeout::Error) from /usr/local/lib/ruby/1.9.1/net/protocol.rb:134:in `rbuf_fill' from /usr/local/lib/ruby/1.9.1/net/protocol.rb:116:in `readuntil' from /usr/local/lib/ruby/1.9.1/net/protocol.rb:126:in `readline' from /usr/local/lib/ruby/1.9.1/net/http.rb:2219:in `read_status_line' from /usr/local/lib/ruby/1.9.1/net/http.rb:2208:in `read_new' from /usr/local/lib/ruby/1.9.1/net/http.rb:1191:in `transport_request' from /usr/local/lib/ruby/1.9.1/net/http.rb:1177:in `request' from /usr/local/lib/ruby/1.9.1/net/http.rb:1170:in `block in request' from /usr/local/lib/ruby/1.9.1/net/http.rb:627:in `start' from /usr/local/lib/ruby/1.9.1/net/http.rb:1168:in `request' from /usr/local/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.12.0/lib/selenium/webdriver/remote/http/default.rb:81:in `response_for' from /usr/local/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.12.0/lib/selenium/webdriver/remote/http/default.rb:43:in `request' from /usr/local/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.12.0/lib/selenium/webdriver/remote/http/common.rb:39:in `call' from /usr/local/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.12.0/lib/selenium/webdriver/remote/bridge.rb:450:in `raw_execute' from /usr/local/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.12.0/lib/selenium/webdriver/remote/bridge.rb:428:in `execute' from /usr/local/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.12.0/lib/selenium/webdriver/remote/bridge.rb:99:in `get' from /usr/local/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.12.0/lib/selenium/webdriver/common/navigation.rb:14:in `to' from /usr/local/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.5.3/lib/watir-webdriver/browser.rb:61:in `goto' from ./search.rb:17:in `visit_company_home_via_google' from ./search.rb:33:in `' 

我认为Chromedriver中有一个错误,它没有正确返回URL。 我让你的例子使用:

 require 'watir-webdriver' class Search attr_accessor :browser, :company_name def initialize @browser = Watir::Browser.start 'http://www.google.com', :chrome end def get_company_url(company, url=nil) @company_name = company @company_url = url @browser.goto "http://www.google.com/search?q=#{company}" link = @browser.div(:id=>'ires').link return nil unless link.exists? @company_url ||= @browser.driver.current_url end def logoff() @browser.close if @browser end end s = Search.new puts s.get_company_url 'Barracuda Networks'