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快速入门(六)中,我们就已经出现 ...
随机推荐
- EXTJS 5 学习笔记2 - Components
1. The Components Hierachy 组件体系 2. XTypes and Lazy Instantiation xtype与延迟初始化 1) 每个compo ...
- Android IPC机制(三)在Android Studio中使用AIDL实现跨进程方法调用
在上一篇文章Android IPC机制(二)用Messenger进行进程间通信中我们介绍了使用Messenger来进行进程间通信的方法.可是我们能发现Messenger是以串行的方式来处理client ...
- React dva 的使用
各大传送门: DvaJS Github DvaJS API与示例 了解 dva- dva = React-Router + Redux + Redux-saga + fetch - dva的初衷是(其 ...
- Xilinx全局时钟
前言 Xilinx系列.ISE环境中,设计复杂工程时全局时钟系统的设计显得尤为重要. 一.时钟网络与全局缓冲 在XilinxFPGA中,时钟网络分为两类:全局时钟网络和I/O区域时钟网络.以全铜工艺实 ...
- 用javascript获得地址栏参数的两种方法
javascript获得地址栏参数. 方法1: <script language="JavaScript"> //取地址栏参数 <!-- function Req ...
- PCIE知识点
引自:http://bbs.eetop.cn/thread-442072-1-1.html 1.从速度上来讲PCIE1.0标准 2.5G(8B/10B),pcie2.0标准 5.0G(8B/10B)p ...
- [Docker] Docker Hub加速
一.缘由: 今天学习Flask,书上建议用Docker,那我就安装了DockerToolBox(WIN10系统只能用toolbox).其中从docker hub拉取ubuntu镜像时 docker p ...
- CocoaPods did not set the base configuration of your project 问题解决方式
今天在使用pod install的时候.出现了 [!] CocoaPods did not set the base configuration of your project because you ...
- JAVA ,JVM 调试
https://blogs.oracle.com/poonam/entry/analysis_of_strange_hotspot_crashes https://blogs.oracle.com/p ...
- maven(1)------使用myeclipse构建maven项目
maven官网:http://maven.apache.org/ 依据官网的说法,Maven是一个采用纯Java编写的开源项目管理工具,基于一个称为项目对象模型(POM)的概念,可以管理项目的生命周期 ...