selenium测试(Java)-- 隐式等待(十)
2016-07-13 23:20
465 查看
隐式等待相当于设置全局的等待,在定位元素时,对所有元素设置超时时间。
隐式等待使得WebDriver在查找一个Element或者Element数组时,每隔一段特定的时间就会轮询一次DOM,如果Element或数组没有马上被发现的话。
默认设置是0。一旦设置,这个隐式等待会在WebDriver对象实例的整个生命周期起作用。一劳永逸。
执行结果:
隐式等待使得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 2 没有找到元素 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' 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
相关文章推荐
- java int与integer的区别
- java垃圾回收算法和垃圾收集器
- java部分关键字理解及解析
- Java获取Cookie特殊字符处理
- 十大排序算法 JAVA代码
- struts2类型转换器、 类型转换错误 以及INPUT view
- java JTextArea 定位到指定行 选中某一行
- Spring MVC集成slf4j-logback
- java简单实现复制 粘贴 剪切功能代码分享
- 搭建Spring MVC+ jdbcTemplate框架
- Java端和JS端的AES加密解密同步
- eclipse 复制 粘贴 保存 等快捷键失效 原因及解决方案
- 学习java 注解总结.
- eclipse中的一些快捷键以及使用小技巧(持续更新)
- selenium测试(Java)-- 显式等待(九)
- java学习总结(16.07.13)java的系统相关类(System类和Runtime类)
- java_reflect_04
- 【SSH】Struts2配置文件学习
- selenium测试(Java)-- 验证信息(八)
- struts2 复杂参数封装