0

请按照以下代码:

driver.get "https://example.com/"
driver.find_element(:class, "button").submit
driver.navigate.refresh
wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
element = wait.until { driver.find_element(:name => "username") }

我写的代码牢记在心,直到包含的页面element : username出现,继续上一页刷新。但似乎我的代码不符合该要求。因此脚本抛出错误如下“

错误

C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.27.2/lib/selenium/webdr
iver/common/wait.rb:57:in `until': timed out after 10 seconds (Unable to locate
element: {"method":"name","selector":"username"})} (Selenium::WebDriver::Error::
TimeOutError)

有什么好主意可以满足我的要求吗?

谢谢,

4

1 回答 1

2

我还没有在 selenium-webdriver 中找到内置的方法来执行此操作,所以我会执行以下操作:

#Submit your first page
driver.get "https://example.com/"
driver.find_element(:class, "button").submit

#Refresh page until your element appears
end_time = Time.now + 10 #seconds
begin
    element = driver.find_element(:name => "username")
rescue Selenium::WebDriver::Error::NoSuchElementError
    if Time.now < end_time
        driver.navigate.refresh
        retry
    end
end

基本上这是试图找到元素。如果未找到,则捕获异常,刷新页面并重试。重复此操作直到达到时间限制hsa。

于 2013-01-25T18:06:42.713 回答