隐式等待相当于设置全局的等待,在定位元素时,对所有元素设置超时时间。

隐式等待使得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)-- 隐式等待(十)的更多相关文章

  1. python+selenium六:隐式等待

    python+selenium六:隐式等待   # 隐式等待 # 全局生效,只写一次即可(仅当前页面)# 若有页面切换,需sleep等待新页面出现后,再使用此方法 # 如:在35秒内,等待操作完成,完 ...

  2. [selenium webdriver Java]隐式的等待同步

    Selenium WebDriver提供了隐式等待来同步测试.当使用了隐式等待执行测试的时候,如果WebDriver没有在DOM中找到元素,将继续等待,超出设定时间后,抛出找不到元素异常 即,当元素没 ...

  3. (java)selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待

    selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待 本例包括窗口最大化,刷新,切换到指定窗口,后退,前进,获取当前窗口url等操作: import java. ...

  4. Selenium+Java显示等待和隐式等待

    描述:用来操作界面上的等待时间,显示等待是等待某一条件满足,条件满足后进行后面的操作:隐式等待是给出一个等待时间,在时间到达之前若满足条件,则立即执行后续操作. public class TestSe ...

  5. Python3 Selenium自动化web测试 ==>FAQ:隐式等待和sleep区别

    FAQ: 情景1: 设置等待时间 A方法:sleep 线程休眠,但只单次有效,其他操作需要加载等待时间,需要再次添加time.sleep() B方法:implicitly_wait() from se ...

  6. 基于Selenium2+Java的UI自动化(8)- 显式等待和隐式等待

    一.隐式等待 package com.automation.waits; import java.util.concurrent.TimeUnit; import org.openqa.seleniu ...

  7. selenium的显示等待和隐式等待的区别

    什么是显示等待和隐式等待?显示等待就是有条件的等待隐式等待就是无条件的等待 隐式等待 当使用了隐式等待执行测试的时候,如果 WebDriver没有在 DOM中找到元素,将继续等待,超出设定时间后则抛出 ...

  8. Selenium 定位元素原理,基本API,显示等待,隐式等待,重试机制等等

    Selenium  如何定位动态元素: 测试的时候会遇到元素每次变动的情况,例如: <div id="btn-attention_2030295">...</di ...

  9. 隐式等待-----Selenium快速入门(九)

    有时候,网页未加载完成,或加载失败,但是我们后续的代码就已经开始查找页面上的元素了,这通常将导致查找元素失败.在本系列Selenium窗口切换-----Selenium快速入门(六)中,我们就已经出现 ...

随机推荐

  1. PLSQL_统计信息系列04_统计信息的锁定和删除

    20150506 Created By BaoXinjian

  2. sklearn 中的交叉验证

    sklearn中的交叉验证(Cross-Validation) sklearn是利用python进行机器学习中一个非常全面和好用的第三方库,用过的都说好.今天主要记录一下sklearn中关于交叉验证的 ...

  3. 江南白衣 Java性能优化PPT

    会后江南白衣花费数日整理PPT,到1.4版本共66页,内容翔实且旁征博引,不断校正且力求做到极致的匠艺精神值得尊敬! 技术大牛飒然Hang如是说到: “技术大神中,除了陈皓就是白衣了,四十岁还能钻研. ...

  4. python标准库介绍——16 shutil模块详解

    ``shutil`` 实用模块包含了一些用于复制文件和文件夹的函数. [Example 2-4 #eg-2-4] 中使用的 ``copy`` 函数使用和 Unix 下 ``cp`` 命令基本相同的方式 ...

  5. Composer的下载安装

    下载地址 https://getcomposer.org/download/ php必须开启php_openssl.dll  在php.ini 1.下载 composer.phar 2.然后配置 ph ...

  6. Ip地址和子网掩码和CIDR无间别域间路由

    开始,网络的制定者将网络划分为A,B,C三种网络,想这个样子: A类网:  xxx.0.0.0         子网掩码:255.0.0.0 xxx.0.0.0/8 //后面的数字代表网络地址的字段 ...

  7. 【Android】11.4 Fragment及其生命周期

    分类:C#.Android.VS2015: 创建日期:2016-02-22 一.简介 Android从3.0开始引入了fragment的概念,主要是为了支持在大屏幕上实现更为动态和灵活的UI设计,比如 ...

  8. 《Android源代码设计模式解析与实战》读书笔记(二十)

    第二十章.适配器模式 适配器模式是结构型设计模式之中的一个,它在我们的开发中使用率极高,比方ListView.GridView以及RecyclerView都须要使用Adapter. 1.定义 适配器模 ...

  9. 关于haproxy负载均衡的算法整理

    目前haproxy支持的负载均衡算法有如下8种: 1:roudrobin 表示简单的轮询,每个服务器根据权重轮流使用,在服务器的处理时间平均分配的情况下这是最流畅和公平的算法.该算法是动态的,对于实例 ...

  10. 关于Snmp的Trap代码开发之坑

    最近是被这个snmp的trap给坑了,其实回想起来主要是对这个不了解.特别是对snmp协议研究不够深入, 真的不想看这些协议东西,只想大概知道.结果在开发snmp trap时候被坑了,下面列下自己踩到 ...