0
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 秒后停止页面加载,然后像我在上面的代码中输入的那样点击元素。因为如果我尝试查看页面源,我检查了页面何时开始加载,我没有得到任何网络元素。但是在页面加载开始后,如果我从开始到 10-12 秒关闭它,并尝试查看网页源代码,那里有所有必需的元素。

所以我想在程序上做同样的事情。知道怎么做吗?

4

1 回答 1

0

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

试试这个代码:

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

上面的代码将等待页面加载长达 10 秒。如果页面加载退出时间,它将抛出TimeoutException. 您捕获异常并满足您的需求。我不确定它是否会在抛出异常后退出页面加载。我还没有尝试过这段代码。想试一试。

了解更多信息。

于 2013-02-05T06:16:23.343 回答