selenium测试(Java)-- 隐式等待(十)
隐式等待相当于设置全局的等待,在定位元素时,对所有元素设置超时时间。
隐式等待使得WebDriver在查找一个Element或者Element数组时,每隔一段特定的时间就会轮询一次DOM,如果Element或数组没有马上被发现的话。
默认设置是0。一旦设置,这个隐式等待会在WebDriver对象实例的整个生命周期起作用。一劳永逸。
package com.test.elementwait; import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait; public class ImplicitWait { public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.baidu.com");
driver.manage().window().maximize(); try {
SimpleDateFormat format = new SimpleDateFormat("HH-mm-ss-SSS");
String time = format.format(Calendar.getInstance().getTime());
System.out.println("开始的时间: " + time); driver.findElement(By.id("kw22")).sendKeys("selenium"); } catch (NoSuchElementException e) {
System.out.println("没有找到元素");
e.printStackTrace();
} finally {
SimpleDateFormat format2 = new SimpleDateFormat("HH-mm-ss-SSS");
String time2 = format2.format(Calendar.getInstance().getTime());
System.out.println("结束的时间: " + time2);
driver.quit();
} } }
执行结果:
开始的时间: 23-12-26-775
没有找到元素
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"kw22"}
Command duration or timeout: 10.46 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
8 Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=45.2.0, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: dda0673c-da3d-4173-a904-d17148a3e26e
*** Element info: {Using=id, value=kw22}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:413)
at org.openqa.selenium.By$ById.findElement(By.java:218)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
at com.test.elementwait.ImplicitWait.main(ImplicitWait.java:26)
结束的时间: 23-12-37-273
selenium测试(Java)-- 隐式等待(十)的更多相关文章
- python+selenium六:隐式等待
python+selenium六:隐式等待 # 隐式等待 # 全局生效,只写一次即可(仅当前页面)# 若有页面切换,需sleep等待新页面出现后,再使用此方法 # 如:在35秒内,等待操作完成,完 ...
- [selenium webdriver Java]隐式的等待同步
Selenium WebDriver提供了隐式等待来同步测试.当使用了隐式等待执行测试的时候,如果WebDriver没有在DOM中找到元素,将继续等待,超出设定时间后,抛出找不到元素异常 即,当元素没 ...
- (java)selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待
selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待 本例包括窗口最大化,刷新,切换到指定窗口,后退,前进,获取当前窗口url等操作: import java. ...
- Selenium+Java显示等待和隐式等待
描述:用来操作界面上的等待时间,显示等待是等待某一条件满足,条件满足后进行后面的操作:隐式等待是给出一个等待时间,在时间到达之前若满足条件,则立即执行后续操作. public class TestSe ...
- Python3 Selenium自动化web测试 ==>FAQ:隐式等待和sleep区别
FAQ: 情景1: 设置等待时间 A方法:sleep 线程休眠,但只单次有效,其他操作需要加载等待时间,需要再次添加time.sleep() B方法:implicitly_wait() from se ...
- 基于Selenium2+Java的UI自动化(8)- 显式等待和隐式等待
一.隐式等待 package com.automation.waits; import java.util.concurrent.TimeUnit; import org.openqa.seleniu ...
- selenium的显示等待和隐式等待的区别
什么是显示等待和隐式等待?显示等待就是有条件的等待隐式等待就是无条件的等待 隐式等待 当使用了隐式等待执行测试的时候,如果 WebDriver没有在 DOM中找到元素,将继续等待,超出设定时间后则抛出 ...
- Selenium 定位元素原理,基本API,显示等待,隐式等待,重试机制等等
Selenium 如何定位动态元素: 测试的时候会遇到元素每次变动的情况,例如: <div id="btn-attention_2030295">...</di ...
- 隐式等待-----Selenium快速入门(九)
有时候,网页未加载完成,或加载失败,但是我们后续的代码就已经开始查找页面上的元素了,这通常将导致查找元素失败.在本系列Selenium窗口切换-----Selenium快速入门(六)中,我们就已经出现 ...
随机推荐
- DBA_实践指南系列9_Oracle Erp R12应用补丁AutoPatch/AutoControl/AutoConfig(案例)
2013-12-09 Created By BaoXinjian
- WEBSHELL跳板REDUH使用说明
原文链接: http://www.fendou.info/network/webshell-proxy-reduh.html reDuh是可以把内网服务器的端口通过http或https隧道转发到本机, ...
- 17monipdb根据IP获得区域
https://www.ipip.net/download.html https://github.com/17mon/csharp IpAndPositionHelper public class ...
- unity hide/show text
using UnityEngine;using System.Collections; public class PlayerController:MonoBehaviour{ public U ...
- 如何把scratch转成一个swf文件或者exe执行文件
scratch作为一款启蒙用的积木式编程软件,非常受人欢迎,但是,现在有一个问题就是,无法将之转变成一个可执行文件,以便和周边的人们分享. 我个人认为把scratch转变为一个可执行的exe文件,并不 ...
- verilog中的default应该赋什么样的值
Q:在状态机的case语句中,最后要加上默认项default,可是我看到有的书上写的是一个确定的状态,有的则是不定态xxx,到底应该写那个啊?求助! A1:取决于case条件是否完备啦如果你的case ...
- 基于Nginx实现10万+并发,你应该做的Linux内核优化
由于默认的linux内核参数考虑的是最通用场景,这明显不符合用于支持高并发访问的Web服务器的定义,所以需要修改Linux内核参数,是的Nginx可以拥有更高的性能: 在优化内核时,可以做的事情很多, ...
- mongoose中的流查询stream query
mongoose中的流查询stream query,功能类似于php中的mysql_fetch_array,每次从集合中获取一条记录(文档) var cursor = Person.find({ oc ...
- brendangregg
http://www.slideshare.net/brendangregg/presentations http://techblog.netflix.com/2015/07/java-in-fla ...
- angular学习笔记(九)-css类和样式2
在上一个例子中,元素的类名使用拼接的方法,这样,类名中就不得不带有true或false,并且不易维护,所以,angular使用ng-class属性来控制元素的类名: 我们来看一个小例子,点击error ...