如今,大多数的Web应用程序使用AJAX技术。当页面加载到浏览器,页面中的元素也许在不同的时间间隔内加载。这使得元素很难定位,如果在DOM中的元素没有呈现,它将抛出ElementNotVisibleException异常。使用waits,我们可以解决这个问题。

  Selenium WebDriver 提供两种类型的waits -- 隐式和显式。显式的wait使webdriver等待发生之前,继续执行一定的条件。一个隐式的wait使webdriver DOM在一定时间后,试图定位元素。

  1. Explicit Waits(显式wait)

     一个显式的wait是你定义等待某个条件发生在代码进一步处理之前的代码。这样做的最坏的情况是time.sleep(),其中规定的条件,以一个确切的时间等待。有一些方便的方法,可帮助您编写wait代码:只需要等待所需要的时间。 WebDriverWait与ExpectedCondition的组合是可以完成这个wait的一种方式。

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Firefox()
    driver.get("http://somedomain/url_that_delays_loading")
    try:
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, "myDynamicElement"))
        )
    finally:
        driver.quit()

    这种wait最多等待10秒后会抛出一个TimeoutException异常,或者如果它在0-10s内发现该元素将返回一次。默认情况下WebDriverWait调用ExpectedCondition每500毫秒,直到它成功返回。一个ExpectedCondition类型的成功的返回类型是布尔返回true或不为null返回值。

    Expected Conditions(预期条件)
    当自动化web浏览器操作时,常频繁遇到一些常见的情况。下面列出每一种实现。Selenium Python Binding 提供了一些简便的方法方便我们没有必要编写 expexted_condition 类或者为他们创建工具包。

    · 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

    from selenium.webdriver.support import expected_conditions as EC
    
    wait = WebDriverWait(driver, 10)
    element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))
  2. Implicit Waits(隐式wait)
    隐式wait是告诉的webdriver在试图定位元素时,如果元素不能立刻可用,则调查DOM一定的时间。默认设置为0。一旦设置,隐式等待被设置为对象的webdriver实例的生命周期。
    from selenium import webdriver
    
    driver = webdriver.Firefox()
    driver.implicitly_wait(10) # seconds
    driver.get("http://somedomain/url_that_delays_loading")
    myDynamicElement = driver.find_element_by_id("myDynamicElement")

<译>Selenium Python Bindings 5 - Waits的更多相关文章

  1. <译>Selenium Python Bindings 1 - Installation

    Installation Introduction Selenium Python bindings 提供了一个简单的API来使用Selenium WebDriver编写使用功能/验收测试.通过Sel ...

  2. <译>Selenium Python Bindings 2 - Getting Started

    Simple Usage如果你已经安装了Selenium Python,你可以通过Python这样使用: #coding=gbk ''' Created on 2014年5月6日 @author: u ...

  3. <译>Selenium Python Bindings 6 - WebDriver API

    本章涉及Selenium WebDriver的所有接口. Recommended Import Style 推荐的导入风格如下: from selenium import webdriver 然后,你 ...

  4. <译>Selenium Python Bindings 4 - Locating Eelements

    有各种不同的策略来定位页面中的元素.你可以使用最合适定位方式用于你的用例.Selenium提供了以下方法来定位页面中的元素: find_element_by_id find_element_by_na ...

  5. <译>Selenium Python Bindings 3 - Navigating

    当你想要通过webdriver导航到一个链接,正常的方式点是通过调用get方法: driver.get("http://www.google.com") Interacting w ...

  6. selenium python bindings 元素定位

    1. 辅助 Firepath Firefox是所有做前端的必不可少的浏览器因为firebug的页面元素显示很清晰.用selenium 去定位元素的时候Firefox还有一个非常友好的工具就是firep ...

  7. [译]Selenium Python文档:五、Waits等待

    大多数现代web应用都使用了AJAX技术.当浏览器加载一个页面的时候,该页面内的元素可能在不用的时间间隔内进行加载.这使得元素定位变得比较困难:如果一个元素还没有出现在DOM中,定位函数将会抛出一个E ...

  8. [译]Selenium Python文档:目录

    作者:Baiju Muthukadan 协议:本文档采用知识共享署名 - 共享4.0国际许可. 原英文网址:http://selenium-python.readthedocs.io/index.ht ...

  9. [译]Selenium Python文档:一、安装

    1.1.简介 Selenium Python为使用Selenium WebDriver来编写功能/验证测试提供了一个简单的API接口.通过Selenium Python API,你可以以一种非常直观的 ...

随机推荐

  1. 解决virtualbox 虚拟机不能ping通win7

    凭经验猜测是由于防火墙引起的,关闭防火墙再ping,果然可行.google说这是由于“win7 防火墙默认的禁ping策略”引起的.但是关闭防火墙很不安全,可以按照以下步骤为防火墙添加入站规则来解决问 ...

  2. Project Euler 89:Roman numerals 罗马数字

    Roman numerals For a number written in Roman numerals to be considered valid there are basic rules w ...

  3. Python str字符串常用到的函数

    # -*- coding: utf-8 -*- x='pythonnnnnnoooo' print type(x) # <type 'str'> 输出类型 print x.capitali ...

  4. JavaWeb项目开发案例精粹-第3章在线考试系统-006实体层

    1. package com.sanqing.po; /* * 学生表,保存学生编号,系统密码 */ public class Student { private String studentID; ...

  5. HTML5 增强的页面元素

    一.HTML5 改良的 input 元素的种类 1.<input type="number" id="num1"> var n1 = documen ...

  6. 74. Search a 2D Matrix

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

  7. C++:向函数传递对象(对象、对象指针、对象引用)

    3.5.1   使用对象作为函数参数,其方法与传递基本类型的变量相同 //例3.21 使用对象作为函数参数 #include<iostream> using namespace std; ...

  8. C++:对象数组

    对象数组 对象数组:每一个数组元素都是对象的数组,也就是说,若一个类有若干个对象,我们把这 一系列的对象用一个数组来存放.对应数组元素是对象,不仅具有的数据成员,而且还有函数 成员. @定义一个一维数 ...

  9. Java API —— JDK5新特性

    JDK5新特性         自动拆装箱.泛型.增强for.静态导入.可变参数.枚举   1.增强for概述         1)简化数组和Collection集合的遍历         2)格式: ...

  10. Java API ——Collection集合类 & Iterator接口

    对象数组举例: 学生类: package itcast01; /** * Created by gao on 15-12-9. */ public class Student { private St ...