反正有没有从浏览器停止页面加载?

时间:2013-02-04 14:54:34

标签: ruby selenium-webdriver

require 'rubygems'
require 'selenium-webdriver'
require 'win32ole'

driver = Selenium::WebDriver.for :firefox
driver.manage.timeouts.implicit_wait = 10
driver.get "https://prod.example.com/"
link_name = wait.until {

    element = driver.find_element(:link_text, "Company")
    element if element.displayed?
}

link_name.click if link_name

但由于页面加载未完成,我的脚本每次都失败,产生以下错误:

C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:146:in `rescue in rbuf_fill': Timeout:
:Error (Timeout::Error)
        from C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:140:in `rbuf_fill'
        from C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:122:in `readuntil'
        from C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:132:in `readline'
        from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:2562:in `read_status_line'
        from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:2551:in `read_new'
        from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1319:in `block in transport_r
equest'
        from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1316:in `catch'
        from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1316:in `transport_request'
        from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1293:in `request'
        from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1286:in `block in request'
        from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:745:in `start'
        from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1284:in `request'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.27.2/lib/s
elenium/webdriver/remote/http/default.rb:83:in `response_for'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.27.2/lib/s
elenium/webdriver/remote/http/default.rb:39:in `request'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.27.2/lib/s
elenium/webdriver/remote/http/common.rb:40:in `call'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.27.2/lib/s
elenium/webdriver/remote/bridge.rb:615:in `raw_execute'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.27.2/lib/s
elenium/webdriver/remote/bridge.rb:593:in `execute'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.27.2/lib/s
elenium/webdriver/remote/bridge.rb:561:in `find_element_by'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.27.2/lib/s
elenium/webdriver/common/search_context.rb:42:in `find_element'
        from C:/Documents and Settings/rakshiar/My Documents/userdata/Ruby/Scrip
ts/answerlist.rb:29:in `rescue in <main>'
        from C:/Documents and Settings/rakshiar/My Documents/userdata/Ruby/Scrip
ts/answerlist.rb:21:in `<main>'

因此我想在20秒之后停止页面加载,然后在我输入上面的代码时点击元素。因为我在页面开始加载时检查了如果我试图查看页面源,我没有得到任何web元素。但是在页面加载开始之后,如果我从开始关闭它到10-12秒,并尝试查看网页源代码,那里所有必需的元素。

所以我想用progamatically做同样的事情。知道怎么做吗?

1 个答案:

答案 0 :(得分:0)

在Selenium2 API中,默认情况下,没有选项可以停止页面加载。它将等待页面完成加载。

试用此代码:

  driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);

以上代码将等待最多10秒的页面加载。如果页面加载退出时将抛出TimeoutException。你抓住了例外,满足了你的需求。我不确定天气会在抛出异常后退出页面加载。我还没有尝试这个代码。想要尝试一下。

了解更多info.