一、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页面等待处理详解的更多相关文章

  1. CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-总目录

    CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-总目录: 0.Windows 10本机下载Xshell,以方便往Linux主机上上传大文件 1.CentOS7+CDH5.14.0安 ...

  2. 【转】Zabbix 3.0 从入门到精通(zabbix使用详解)

    [转]Zabbix 3.0 从入门到精通(zabbix使用详解) 第1章 zabbix监控 1.1 为什么要监控 在需要的时刻,提前提醒我们服务器出问题了 当出问题之后,可以找到问题的根源   网站/ ...

  3. Java6.0中Comparable接口与Comparator接口详解

    Java6.0中Comparable接口与Comparator接口详解 说到现在,读者应该对Comparable接口有了大概的了解,但是为什么又要有一个Comparator接口呢?难道Java的开发者 ...

  4. python selenium 三种等待方式详解[转]

    python selenium 三种等待方式详解   引言: 当你觉得你的定位没有问题,但是却直接报了元素不可见,那你就可以考虑是不是因为程序运行太快或者页面加载太慢造成了元素不可见,那就必须要加等待 ...

  5. ASP.NT运行原理和页面生命周期详解及其应用

    ASP.NT运行原理和页面生命周期详解及其应用 1. 下面是我画的一张关于asp.net运行原理和页面生命周期的一张详解图.如果你对具体不太了解,请参照博客园其他帖子.在这里我主要讲解它的实际应用.  ...

  6. guava-retrying 源码解析(等待策略详解)

    一.等待策略相关类: 1.等待策略接口:WaitStrategy接口 该接口只有一个方法,就是返回尝试失败之后,下一次尝试之前的等待时间.long computeSleepTime(Attempt f ...

  7. laravel 框架配置404等异常页面的方法详解(代码示例)

    本篇文章给大家带来的内容是关于laravel 框架配置404等异常页面的方法详解(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 在Laravel中所有的异常都由Handl ...

  8. CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-8CDH5安装和集群配置

    Cloudera Manager Server和Agent都启动以后,就可以进行CDH5的安装配置了.      准备文件 从 http://archive.cloudera.com/cdh5/par ...

  9. Zabbix 3.0 从入门到精通(zabbix使用详解)

    第1章 zabbix监控 1.1 为什么要监控 在需要的时刻,提前提醒我们服务器出问题了 当出问题之后,可以找到问题的根源   网站/服务器 的可用性 1.1.1 网站可用性 在软件系统的高可靠性(也 ...

随机推荐

  1. 省市选择(基于zepto.js)

    效果如下: <div class="clList overflow-h mt75"> <select class="pull-left cl-35 se ...

  2. Delphi中的GetEnumName和GetEnumValue的使用方法

    利用TypInfo单元的GetEnumName和GetEnumValue可以遍历任意枚举类型,并获取其名称和值.下面是示例Demo. uses TypInfo; ... procedure TForm ...

  3. web2py--------------用web2py写 django的例子 --------建立一个投票应用(1)

    按照上一篇我们新建一个名为  polls 的app 然后文件结构如下 然后web2py 会自动向里边添加一些代码. 我们需要剔除一些,如这个 controllers ,defualt.py  的ind ...

  4. sbrk and coreleft

    一.sbrk 函数来源:TC2.0.Linux 函数名: sbrk 功 能: 增加程序可用数据段空间,增加大小由参数 incr决定 . 返回值:函数调用成功返回一指针,指向新的内存空间.函数调用失败则 ...

  5. SQLServer数据库通用访问类

    private static string connString=ConfigurationManager.ConnStrings["connString"].ToString() ...

  6. display:inline-block元素间空白间隙问题

    display:inline-block元素间有空白间隙,可以在父元素上加font-size:0

  7. python 读取SQLServer数据插入到MongoDB数据库中

    # -*- coding: utf-8 -*-import pyodbcimport osimport csvimport pymongofrom pymongo import ASCENDING, ...

  8. 解决Matlab启动时 " Can't check 9.0 VCRTs The application has failed to start because……" 的错误

    注:转载或引用请注明出处 今天在winserver 2012 r2 上安装matlab 2016b , 安装完成运行时提示: ERROR: Cnn't check 9.0 VCRTs <star ...

  9. python re.sub

    python re.sub   python re.sub属于python正则的标准库,主要是的功能是用正则匹配要替换的字符串然后把它替换成自己想要的字符串的方法下面给个例子:import relin ...

  10. Jquery 操作Html 控件 CheckBox、Radio、Select 控件

    在使用 Javascript 编写前台脚本的时候,经常会操作 Html 控件,比如 checkbox.radio.select,用 Jquery 库操作其他会方便很多,下面用Jq对这些控件的操作进行一 ...