selenium1.0和selenium2.0页面等待处理详解
一、selenium1.0页面等待
1、……AndWait
经常会看到, selenium action命令中很多有这种……AndWait后缀, 例如click和clickAndWait命令:
click命令:点击操作后, 直接进入下一个动作, 不做等待;
clickAndWait 命令,则是在click之后,自动执行一次waitForPageToLoad,等待直到当前窗口载入一个新页面, 然而,这种等待只适合那些会引起页面刷新的,如果页面不是在当前窗口载入(比如是在一个弹出窗口,或者在一个iframe里载入),那么用AndWait是无法做到正确等待的,它会一直等待当前窗口的页面载入直到超时,这种情况造成白白等待+timeout警告,另外常见的Ajax效果的界面, 使用……AndWait来等待也不起效
还有typeAndWait selectAndWait等等
2、waitFor……
比较好的等待方式是使用waitFor……来等待需要的元素出现,例如,waitForElementPresent, 默认等待30秒, 在30秒内,如果元素已经出现, 马上响应,否则报超时,可以通过IDE里options->options……中General tab来设置默认的timeout时间。
这里摘录了下官网文档中对 “不做等待”,AndWait, waitFor三种方式的阐述:
//1)The difference between a command and its AndWait alternative is that the regular command (e.g. click) will do the action and continue with the following command as fast as it can, while the AndWait alternative (e.g. clickAndWait) tells Selenium to wait for the page to load after the action has been done.
//2)The AndWait alternative is always used when the action causes the browser to navigate to another page or reload the present one.
//3)In AJAX driven web applications, data is retrieved from server without refreshing the page. Using andWait commands will not work as the page is not actually refreshed,,,The best approach would be to wait for the needed element in a dynamic period and then continue the execution as soon as the element is found.as waitForElementPresent orwaitForVisible
二、selenium2.0等待机制
1、显性等待
第一种最简单的方法是: Thread.sleep(1000)
这种设定了固定时间,若时间设置短了, 内容来不及更新, 设置长了延长了脚本运行时间,所以不推荐
第二种方式:采用 WebDriverWait类+ExpectedConditions接口,具体用法例子:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement e1 = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid"))); WebElement e2=wait.until(new ExpectedCondition<WebElement>() {
public WebElement apply(WebDriver d){
return d.findElement(By.id("id locator"));
}
});
说明:这种方式类似seleniu1.0中的waitFor……, 上面代码表示默认等待1-10s, 10内,若ExpectedConditions找到元素立刻返回,超过是10s报超时。
2、隐性等待
WebDriver driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
说明:
执行driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);后
当要查找元素,而这个元素没有马上出现时,会告诉WebDriver查询Dom一定时间(设置了10s)。默认值是0, 设置之后,这个时间将在WebDriver对象实例整个生命周期都起作用
三、附录
这里列出常用的waitFor命令:
|
waitForElementPresent
|
locator
|
等待直到某个元素在页面中存在
|
|
|
waitForElementNotPresent
|
locator
|
等待直到某个元素在页面中不存在
|
|
waitForTextPresent
|
text
|
等待直到某个文本在页面中存在
|
|
|
waitForTextNotPresent
|
text
|
等待直到某个文本在页面中不存在
|
|
waitForText
|
locator
|
text
|
等待直到locator指定元素内的文本符合text
|
|
waitForNotText
|
locator
|
text
|
等待直到locator指定元素内的文本不符合text
|
|
waitForVisible
|
locator
|
等待直到locator指定元素在页面中可见
|
|
|
waitForNotVisible
|
locator
|
等待直到locator指定元素在页面中不可见
|
|
waitForTitle
|
title
|
等待直到页面标题符合所指定的title
|
|
|
waitForNotTitle
|
title
|
等待直到页面标题不符合所指定的title
|
|
waitForLocation
|
url
|
等待直到页面的地址符合所指定的url
|
|
|
waitForNotLocation
|
url
|
等待直到页面的地址不符合所指定的url
|
|
waitForValue
|
locator
|
value
|
等待直到locator指定的元素的value值符合指定的值
|
|
waitForNotValue
|
locator
|
value
|
等待直到locator指定的元素的value值不符合指定的值
|
|
waitForAttribute
|
value
|
等待直到locator指定的元素的attr属性值符合指定的值
|
|
|
waitForNotAttribute
|
value
|
等待直到locator指定的元素的attr属性值不符合指定的值
|
|
waitForChecked
|
locator
|
等待直到locator指定的radio或checkbox成为选中状态
|
|
|
waitForNotChecked
|
locator
|
等待直到locator指定的radio或checkbox成为非选中状态
|
|
waitForEditable
|
locator
|
等待直到locator指定的input或textarea元素可编辑
|
|
|
waitForNotEditable
|
locator
|
等待直到locator指定的input或textarea元素不可编辑
|
|
waitForXpathCount
|
xpath
|
count
|
等待直到页面符合xpath的数量为count
|
|
waitForNotXpathCount
|
xpath
|
count
|
等待直到页面符合xpath的数量不为count
|
|
waitForEval
|
script
|
pattern
|
等待直到script的执行结果符合pattern
|
|
waitForNotEval
|
script
|
pattern
|
等待直到script的执行结果不符合pattern
|
selenium1.0和selenium2.0页面等待处理详解的更多相关文章
- CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-总目录
CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-总目录: 0.Windows 10本机下载Xshell,以方便往Linux主机上上传大文件 1.CentOS7+CDH5.14.0安 ...
- 【转】Zabbix 3.0 从入门到精通(zabbix使用详解)
[转]Zabbix 3.0 从入门到精通(zabbix使用详解) 第1章 zabbix监控 1.1 为什么要监控 在需要的时刻,提前提醒我们服务器出问题了 当出问题之后,可以找到问题的根源 网站/ ...
- Java6.0中Comparable接口与Comparator接口详解
Java6.0中Comparable接口与Comparator接口详解 说到现在,读者应该对Comparable接口有了大概的了解,但是为什么又要有一个Comparator接口呢?难道Java的开发者 ...
- python selenium 三种等待方式详解[转]
python selenium 三种等待方式详解 引言: 当你觉得你的定位没有问题,但是却直接报了元素不可见,那你就可以考虑是不是因为程序运行太快或者页面加载太慢造成了元素不可见,那就必须要加等待 ...
- ASP.NT运行原理和页面生命周期详解及其应用
ASP.NT运行原理和页面生命周期详解及其应用 1. 下面是我画的一张关于asp.net运行原理和页面生命周期的一张详解图.如果你对具体不太了解,请参照博客园其他帖子.在这里我主要讲解它的实际应用. ...
- guava-retrying 源码解析(等待策略详解)
一.等待策略相关类: 1.等待策略接口:WaitStrategy接口 该接口只有一个方法,就是返回尝试失败之后,下一次尝试之前的等待时间.long computeSleepTime(Attempt f ...
- laravel 框架配置404等异常页面的方法详解(代码示例)
本篇文章给大家带来的内容是关于laravel 框架配置404等异常页面的方法详解(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 在Laravel中所有的异常都由Handl ...
- CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-8CDH5安装和集群配置
Cloudera Manager Server和Agent都启动以后,就可以进行CDH5的安装配置了. 准备文件 从 http://archive.cloudera.com/cdh5/par ...
- Zabbix 3.0 从入门到精通(zabbix使用详解)
第1章 zabbix监控 1.1 为什么要监控 在需要的时刻,提前提醒我们服务器出问题了 当出问题之后,可以找到问题的根源 网站/服务器 的可用性 1.1.1 网站可用性 在软件系统的高可靠性(也 ...
随机推荐
- c/c++中的各种字符串转换
一:CString 和 *char 的转换: 1:CString -> *char 1)CString转化为*char可以使用CString中的GetBuffer()函数,具体如下: CStri ...
- j2ee中如何拦截jsp页面?
加filter: public class RightFilter implements Filter { public void init(FilterConfig filterConfig) th ...
- (转载)SQL中导入图片
SQL中导入图片 分类: 论坛精贴 2006-05-10 12:07 398人阅读 评论(0) 收藏 举报 sqlimage服务器insertlogingo 1.建立过程CREATE PROCEDUR ...
- 温故知新——json
Json简介 Json(JavaScript Object Notation)是一种轻量级的数据交换格式.它是基于javascript(Standard ECMA-262 3rd Edition - ...
- Ubuntu 下部署asp.net运行环境
在Ubuntu下部署asp.net运行环境,网上教程很多,基本都是编译Mono源码,然后安装jexus.但是可能是我最近RP不太好,编译Mono源码一直都是失败,无奈之下只好找另外的方法安装了. 网上 ...
- FontDialog组件设置字体
1.设置字体 private void button3_Click(object sender, EventArgs e) { this.fontDialog1.ShowDialog(); this. ...
- 【@Transactional】Spring 之注解事务 @Transactional
spring 事务注解 默认遇到throw new RuntimeException("...");会回滚 需要捕获的throw new Exception("...&q ...
- Load a script file in sencha, supports both asynchronous and synchronous approaches
//http://www.sencha.com/forum/showthread.php?188318-Ext.Loader.loadScriptFile-wrong-URL Ext.Loader.l ...
- 如何学习C++[转]
关于学C++, 我向你推荐一些书(当然能够结合课内项目实践更好) 1.The C++ Programming Language(Bjarne Stroustrup)2. Inside The C++ ...
- java.util.List
/* * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETA ...