Selenium WebDriver:等待document.readyState

时间:2013-10-31 15:51:56

标签: ruby selenium selenium-webdriver

这里有什么问题?我想等待下一个任务,直到我的页面完全加载。 问题:没有错误,但是驱动程序没有等待:-(

# wait for page load
wait2 = Selenium::WebDriver::Wait.new(:timeout => 20) # seconds
count = 0
begin
  raise("maximum attempt crossed #{count} times") if count > 3
  wait2.until {
    self.getDriver.execute_script("return document.readyState;") == "complete"
  }
rescue Timeout::Error
  count +=1
  retry
end

#do sth

2 个答案:

答案 0 :(得分:0)

我会按如下方式解决问题:

wait = Selenium::WebDriver::Wait.new(:timeout => 20) # seconds
count = 0
begin
   raise("maximum attempt crossed #{count} times") if count > 3
   # if the page having title 'page_title' is not loaded within 20 seconds
   # time out error will be thrown,which will be handled by the rescue clause.
   wait.until { driver.title == 'page_title' }
rescue Timeout::Error
   count +=1
   retry
end

答案 1 :(得分:0)

这似乎对我有用。

DEFAULT_TIMEOUT = 10

def wait_for_page_to_load(options = {})
    options[:timeout] ||= DEFAULT_TIMEOUT

    wait = Selenium::WebDriver::Wait.new(options)
    wait.until {current_driver.execute_script('var browserState = document.readyState; return browserState;') == "complete" }

end