5. Waits

Waits

目前大部分Web应用都使用的是AJAX技术。当一个页面被加载到浏览器时,这个页面的元素可能在不同时间段进行加载。 如果元素不存在与DOM中,将很难被定位到并将会报出

ElementNotVisibleException 异常。我们可以使用waits来解决这个问题。Waiting 操作执行之间提供了时间的间隔。

Selenium Webdriver 提供了两种 waits:implicit & explicit.

Explicit Waits(显示等待)

定义一个等待的条件,当到达这个条件后,结束等待。



Expected Conditions

  • title_is
  • title_contains
  • presence_of_element_located
  • visibility_of_element_located
  • visibility_of
  • presence_of_all_elements_located
  • text_to_be_present_in_element
  • text_to_be_present_in_element_value
  • frame_to_be_available_and_switch_to_it
  • invisibility_of_element_located
  • element_to_be_clickable - it is Displayed and Enabled.
  • staleness_of
  • element_to_be_selected
  • element_located_to_be_selected
  • element_selection_state_to_be
  • element_located_selection_state_to_be
  • alert_is_present

 

Implicit Waits(隐式等待)

定义一个等待的时间。默认值为0s.

 

Wait类的使用场景是在页面上进行某些操作,然后页面上就会出现或隐藏一些元素,此时使用Wait类的until方法来等待这些效果完成以便进行 后续的操作。另外页面加载时有可能会执行一些ajax,这时候也需要去WebDriverWait的until的等待ajax的请求执行完毕。

具体一点的例子前面也曾出现过,点击一个链接然后会出现一个下拉菜单,此时需要先等待下拉菜单出现方可进行点击菜单项的操作。

这时候就需要用到selenium.webdriver.support.ui.WebDriverWait类,实例化该类时可以传入timeout的时间,单位是s。

until方法会一直等下去,直到

  • 代码块中的内容为true(不为false或没有抛出异常)
  • 超时,也就是超过了timeout设置的时间

 

wait.html

  <html>
    <head>
      <meta http-equiv="content-type" content="text/html;charset=utf-8" />
      <title>wait</title>       
      <script type="text/javascript" async="" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
      <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />        
      <script type="text/javascript">
        $(document).ready(function(){
            $('#btn').click(function(){
              $('<p><span class="label label-info">waitr-webdriver</span></p>').css('margin-top', '1em').insertAfter($(this));
              $(this).addClass('disabled').unbind('click');
            });
        });
      </script>
    </head>

    <body>
      <div class="row-fluid">
        <div class="span6 well">        
          <h3>wait</h3>
          <button class="btn btn-primary" id="btn" >Click</button>
        </div>      
      </div>        
    </body>
    <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
  </html>

wait.py

# -*- coding: utf-8 -*- 
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
import os
import selenium.webdriver.support.ui as ui
if 'HTTP_PROXY'in os.environ: del os.environ['HTTP_PROXY']

dr = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('wait.html')

dr.get(file_path)

# 点击按钮
dr.find_element_by_id('btn').click()

wait = ui.WebDriverWait(dr, 10)
wait.until(lambda dr: dr.find_element_by_class_name('label').is_displayed())

sleep(2)
dr.quit()

posted @ 2014-07-18 15:21  hugh007  阅读(199)  评论(0编辑  收藏  举报