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 秒关闭它,并尝试查看网页源代码,那里有所有必需的元素。
所以我想在程序上做同样的事情。知道怎么做吗?