简体   繁体   中英

Watir/Selenium - browser.goto keep getting TimeOut error on Chrome and Firefox

Got a very annoying problem with Watir webdriver..

I have debugged a little, and found out I always get TimeOut::Error on a simple @browser.goto line, even I can visually see the page has loaded fully...

The scenario is like this: Open a browser, goto a url, click a few links, and then suddenly at one point, the script stops continue browsing, waiting about 30+ seconds and throw errors. Tried both Chrome and FF: Chrome is much worse, normally a 2nd or 3nd link clicking will trigger; for FF, sometimes takes 10+ page browsing...

Bet there is some environment or comparability issue:

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)

Can someone help on this? Source code here:

#!/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

My result is like:

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 `<main>'

I think there's a bug in Chromedriver that doesn't return the URL correctly. I got your example to work using:

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM