<译>Selenium Python Bindings 5 - Waits
如今,大多数的Web应用程序使用AJAX技术。当页面加载到浏览器,页面中的元素也许在不同的时间间隔内加载。这使得元素很难定位,如果在DOM中的元素没有呈现,它将抛出ElementNotVisibleException异常。使用waits,我们可以解决这个问题。
Selenium WebDriver 提供两种类型的waits -- 隐式和显式。显式的wait使webdriver等待发生之前,继续执行一定的条件。一个隐式的wait使webdriver DOM在一定时间后,试图定位元素。
- 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_presentfrom selenium.webdriver.support import expected_conditions as EC wait = WebDriverWait(driver, 10) element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))
- 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的更多相关文章
- <译>Selenium Python Bindings 1 - Installation
Installation Introduction Selenium Python bindings 提供了一个简单的API来使用Selenium WebDriver编写使用功能/验收测试.通过Sel ...
- <译>Selenium Python Bindings 2 - Getting Started
Simple Usage如果你已经安装了Selenium Python,你可以通过Python这样使用: #coding=gbk ''' Created on 2014年5月6日 @author: u ...
- <译>Selenium Python Bindings 6 - WebDriver API
本章涉及Selenium WebDriver的所有接口. Recommended Import Style 推荐的导入风格如下: from selenium import webdriver 然后,你 ...
- <译>Selenium Python Bindings 4 - Locating Eelements
有各种不同的策略来定位页面中的元素.你可以使用最合适定位方式用于你的用例.Selenium提供了以下方法来定位页面中的元素: find_element_by_id find_element_by_na ...
- <译>Selenium Python Bindings 3 - Navigating
当你想要通过webdriver导航到一个链接,正常的方式点是通过调用get方法: driver.get("http://www.google.com") Interacting w ...
- selenium python bindings 元素定位
1. 辅助 Firepath Firefox是所有做前端的必不可少的浏览器因为firebug的页面元素显示很清晰.用selenium 去定位元素的时候Firefox还有一个非常友好的工具就是firep ...
- [译]Selenium Python文档:五、Waits等待
大多数现代web应用都使用了AJAX技术.当浏览器加载一个页面的时候,该页面内的元素可能在不用的时间间隔内进行加载.这使得元素定位变得比较困难:如果一个元素还没有出现在DOM中,定位函数将会抛出一个E ...
- [译]Selenium Python文档:目录
作者:Baiju Muthukadan 协议:本文档采用知识共享署名 - 共享4.0国际许可. 原英文网址:http://selenium-python.readthedocs.io/index.ht ...
- [译]Selenium Python文档:一、安装
1.1.简介 Selenium Python为使用Selenium WebDriver来编写功能/验证测试提供了一个简单的API接口.通过Selenium Python API,你可以以一种非常直观的 ...
随机推荐
- Openflow的转发与传统的转发区别和优势
来源:(SDN QQ群语录20130819) http://www.sdnap.com/sdnap-post/2411.html 山东同学-菜(Q群279796875) 21:40:21我是想问,op ...
- 关于in与exists的效率讨论
关于in与exists的效率讨论1).select * from A where id in (select id from B)以上查询使用了in语句,in只执行一次,他查出B表的所有id字段并缓存 ...
- lintcode:三数之和
题目 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集 ...
- HDU5088——Revenge of Nim II(高斯消元&矩阵的秩)(BestCoder Round #16)
Revenge of Nim II Problem DescriptionNim is a mathematical game of strategy in which two players tak ...
- Android Handler传值方式
前面介绍handler的时候,也用到过几种传值方式,今天来总结一下,并且重点说一下bundle方式,代码如下: package com.handlerThread; import android.ap ...
- 转 Difference between WCF and Web API and WCF REST and Web Service
http://www.dotnet-tricks.com/Tutorial/webapi/JI2X050413-Difference-between-WCF-and-Web-API-and-WCF-R ...
- Java API —— Set接口 & HashSet类 & LinkedHashSet类
1.Set接口 1)Set接口概述 一个不包含重复元素的 collection,无序(存储顺序和取出顺序不一致),唯一. (List有序,即存储顺序和取出顺序一致,可重复) ...
- Xcode学习
http://www.cnblogs.com/ygm900/p/3488881.html
- WCF学习笔记之地址
1.统一资源标识(URI) URI全称是Uniform Resource Identifier(统一资源标识),唯一地标识一个网络资源的同时也标识资源所处的位置以及访问方式(资源访问所用的网络协议). ...
- MyEclipse 常用设置
1.指定本机java环境 Windows-->preferences-->java-->Insetallel JREs 右侧 单击ADD-->standard VM--> ...