<译>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,你可以以一种非常直观的 ...
随机推荐
- Ubuntu 装JDK
我是按照这篇文章安装jdk的: http://www.cnblogs.com/bluestorm/archive/2012/05/10/2493592.html 先去 Oracle下载Linux下 ...
- linux 查看局域网内ip
$ sudo apt-get install nmap $ nmap -sP 192.168.1.1/24 windows 下直接arp -a就能看到.
- Tomcat处理HTTP请求源码分析(下)
转载:http://www.infoq.com/cn/articles/zh-tomcat-http-request-2 很多开源应用服务器都是集成tomcat作为web container的,而且对 ...
- 利用Nginx搭建http和rtmp协议的流媒体服务器
http://www.linuxidc.com/Linux/2013-02/79118.htm
- JAVA学习路线图---(JAVA1234)
第一阶段-Java基础 这一阶段很重要,关系到你后面阶段的学习,所以务必把这一阶段掌握好: 如果你是0基本,我推荐一本比较好的,适合初学者看的书:明日科技的<Java从入门到精通>,最 ...
- latex 三线表
LaTeX 处理三线表相当简单方便.用到的宏包主要是 booktabs .代码如下: 需要添加包:\usepackage{booktabs}. \documentclass{article} \use ...
- Internet Explorer for Mac the Easy Way: Run IE 7, IE8, & IE9 Free in a Virtual Machine
From link: http://osxdaily.com/2011/09/04/internet-explorer-for-mac-ie7-ie8-ie-9-free/ If you’re ...
- bzoj3242
如果是树,那么一定选择树的直径的中点 套了个环?裸的想法显然是断开环,然后求所有树的直径的最小值 环套树dp的一般思路,先把环放到根,把环上点下面的子树dp出来,然后再处理环上的情况 设f[i]表示以 ...
- js判断浏览器类型和内核
function judge() { var sUserAgent = navigator.userAgent.toLocaleLowerCase(); var isLinux = (String(n ...
- [Sciter系列] MFC下的Sciter–4.HTML与图片资源内置
[Sciter系列] MFC下的Sciter–4.HTML与图片资源内置,防止代码泄露. 本系列文章的目的就是一步步构建出一个功能可用,接口基本完善的基于MFC框架的SciterFrame程序,以此作 ...