如今,大多数的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. *[topcoder]TaroFriends

    http://community.topcoder.com/stat?c=problem_statement&pm=13005 好题.最暴力是试验2^n种跳法.然后有从结果入手,那么最终的左右 ...

  2. POJ1118 Lining Up

    快弄死我了 最后的原因是abs和fabs的区别... 说点收获:1.cmp函数返回的是int,所以不要直接返回double相减的结果2.define inf 1e9和eps 1e-93.在整数相除得到 ...

  3. LR_问题_运行场景时提示scripts you are running in invalid

    问题描述 脚本在virtual user generator中运行正常. 在Controller中运行场景时报错: the target you defined cannot be reached. ...

  4. HorseCome

    紫气东来,祝福也随之而来,喜鹊登梅,福禄也登上眉梢,马年将至,喜庆将萦绕身旁,在这个美好的日子送上我最真挚的祝福,祝身体安康. 春晓,春晓,处处绿杨芳草.山山水水,欢欢笑笑,共祝六合同春,步步登高!

  5. React-Flux 介绍及实例演示

    一.Flux架构 二.例子 1.TodoApp.react.js /** * Copyright (c) 2014-2015, Facebook, Inc. * All rights reserved ...

  6. Android权限安全(8)ContentProvider基于URI的安全

    一.provider可以通过binder得到客户的uid,然后进程权限检查. 二,provider临时权限 场景:  Email的内容在provider中提供,Email的客户端可读基其内容,现在一封 ...

  7. createElement 创建DOM元素

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...

  8. EF DataBase First生成model的验证

    如何避免在EF自动生成的model中的DataAnnotation被覆盖掉 相信很多人刚接触EF+MVC的时候,DataBase First模式生成model类中加验证信息的时候,会在重新生成mode ...

  9. MetadataType的使用,MVC的Model层数据验证

    MetadataType的使用,MVC的Model层数据验证 指定要与数据模型类关联的元数据类   using System.ComponentModel.DataAnnotations; //指定要 ...

  10. error: qrc_qml.obj: requires unsupported dynamic reloc R_ARM_REL32; recompile with -fPIC解决办法

    使用qtcreator加androidndk编译项目时报错: error: qrc_qml.obj: requires unsupported dynamic reloc R_ARM_REL32; r ...