自动化测试中,等待时间的运用占据了举足轻重的地位,平常我们需要处理很多和时间息息相关的场景,例如:

  • 打开新页面,只要特定元素出现而不用等待页面全部加载完成就对其进行操作
  • 设置等待某元素出现的时间,超时则抛出异常
  • 设置页面加载的时间
  • .....

webdriver类中有三个和时间相关的方法:
  1.pageLoadTimeout
  2.setScriptTimeout
  3.implicitlyWait

我们就从这里开始,慢慢揭开他神秘的面纱。

pageLoadTimeout

pageLoadTimeout方法用来设置页面完全加载的超时时间,完全加载即页面全部渲染,异步同步脚本都执行完成。前面的文章都是使用get方法登录安居客网站,大家应该能感觉到每次打开网页后要等很长一段时间才会进行下一步的操作,那是因为没有设置超时时间而get方法默认是等待页面全部加载完成才会进入下一步骤,加入将超时时间设置为3S就会中断操作抛出异常

 import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement; public class NewTest{
public static void main(String[] args) throws InterruptedException {
System.setProperty ( "webdriver.chrome.driver" ,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
try{
//设置超时时间为3S
driver.manage().timeouts().pageLoadTimeout(3, TimeUnit.SECONDS);
driver.get("http://shanghai.anjuke.com"); WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']"));
input.sendKeys("selenium"); }catch(Exception e){
e.printStackTrace();
}finally{
Thread.sleep(3000);
driver.quit();
}
}

ps:如果时间参数为负数,效果没有使用这个方法是一样的,接口注释中有相关说明:"If the timeout is negative, page loads can be indefinite".

如果想在抛出异常后并不中断而是继续执行下面的操作那该怎么办呢?可以使用try,catch的组合来实现这个功能

 import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement; public class NewTest{
public static void main(String[] args) throws InterruptedException {
System.setProperty ( "webdriver.chrome.driver" ,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
try{
//设置超时时间为3S
driver.manage().timeouts().pageLoadTimeout(3, TimeUnit.SECONDS);
driver.get("http://shanghai.anjuke.com");
}catch(Exception e){ }finally{
WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']"));
if(input.isDisplayed())
input.sendKeys("selenium");
}
}

这样,当页面加载3S后就会执行下面的操作了。

setScriptTimeout

设置异步脚本的超时时间,用法同pageLoadTimeout一样就不再写了,异步脚本也就是有async属性的JS脚本,可以在页面解析的同时执行。

implicitlyWait

识别对象的超时时间,如果在设置的时间类没有找到就抛出一个NoSuchElement异常,用法参数也是和pageLoadTimeout一样,大家可以自己试验试验。

上文中介绍了如何才能在使用pageLoadTimeout抛出异常的同时继续执行,但是现有的方法还是不完美,如果输入框在3S后没有出现还是会报错,怎么办呢?机智的同学可以想到用sleep方法来实现,为了方便演示换个更明显的需求来说明。安居客的首页上有个切换城市的链接,当点击城市的时候就会显示全部的城市以供选择这时display属性就会变化

 import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions; public class NewTest{
public static void main(String[] args) throws InterruptedException {
System.setProperty ( "webdriver.chrome.driver" ,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
try{
//设置超时时间为3S
driver.manage().timeouts().pageLoadTimeout(3, TimeUnit.SECONDS);
driver.get("http://shanghai.anjuke.com");
}catch(Exception e){ }finally{
WebElement city=driver.findElement(By.xpath("//a[@id='switch_apf_id_8']"));
WebElement citys=driver.findElement(By.xpath("//div[@id='city-panel']"));
Actions actions=new Actions(driver);
actions.clickAndHold(city).perform();
while(citys.isDisplayed()){
System.out.println("sleep");
Thread.sleep(3000);
}
Thread.sleep(3000);
driver.quit();
} }

执行后会每过3S就会输出一次sleep,但是这样的代码显然不够高大上,而且没有限制会一直无限的等待下去,下面介绍一种高大上的方法,就是until,先看代码

 import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedCondition; public class NewTest{
public static void main(String[] args) throws InterruptedException {
System.setProperty ( "webdriver.chrome.driver" ,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
try{
//设置超时时间为3S
driver.manage().timeouts().pageLoadTimeout(3, TimeUnit.SECONDS);
driver.get("http://shanghai.anjuke.com");
}catch(Exception e){ }finally{
WebElement city=driver.findElement(By.xpath("//a[@id='switch_apf_id_8']"));
Actions actions=new Actions(driver);
actions.clickAndHold(city).perform(); //最多等待10S,每2S检查一次
WebDriverWait wait=new WebDriverWait(driver,10,2000); wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
System.out.println("sleep");
return !driver.findElement(By.xpath("//div[@id='city-panel']")).isDisplayed();
}
}); Thread.sleep(3000);
driver.quit();
} }

效果是每隔2S输出一个sleep,输出5次后抛出超时异常,简单明了,高大上。

selenium webdriver(5)---超时设置的更多相关文章

  1. Selenium webdriver firefox 路径设置问题

    问题: Cannot find firefox binary in PATH. Make sure firefox is installed. 原因:selenium找不到Firefox浏览器. 方法 ...

  2. Python脚本控制的WebDriver 常用操作 <二十八> 超时设置和cookie操作

    超时设置 测试用例场景 webdriver中可以设置很多的超时时间 implicit_wait.识别对象时的超时时间.过了这个时间如果对象还没找到的话就会抛出异常 Python脚本 ff = webd ...

  3. selenium webdriver——设置元素等待

    如今大多数Web应用程序使用ajax技术,当浏览器在加载页面时,页面上的元素可能并不是同时被加载完成,这给定位元素的定位增加了困难, 如果因为在加载某个元素时延迟而造成ElementNotVisibl ...

  4. Python3 Selenium WebDriver网页的前进、后退、刷新、最大化、获取窗口位置、设置窗口大小、获取页面title、获取网页源码、获取Url等基本操作

    Python3 Selenium WebDriver网页的前进.后退.刷新.最大化.获取窗口位置.设置窗口大小.获取页面title.获取网页源码.获取Url等基本操作 通过selenium webdr ...

  5. selenium - webdriver - 设置元素等待

    隐式等待:implicitly_wait(value), value默认是0 from selenium import webdriverfrom selenium.common.exceptions ...

  6. Python爬虫之设置selenium webdriver等待

    Python爬虫之设置selenium webdriver等待 ajax技术出现使异步加载方式呈现数据的网站越来越多,当浏览器在加载页面时,页面上的元素可能并不是同时被加载完成,这给定位元素的定位增加 ...

  7. Python selenium webdriver设置js操作页面滚动条

    js2 = "window.scrollTo(0,0);" #括号中为坐标 当不知道需要的滚动的坐标大小时: weizhi2 = driver.find_element_by_id ...

  8. Selenium webdriver Java firefox 路径设置问题

    问题: Cannot find firefox binary in PATH. Make sure firefox is installed. 原因:selenium找不到Firefox浏览器. 方法 ...

  9. 转:python webdriver API 之设置等待时间

    有时候为了保证脚本运行的稳定性,需要脚本中添加等待时间.sleep(): 设置固定休眠时间. python 的 time 包提供了休眠方法 sleep() , 导入 time 包后就可以使用 slee ...

随机推荐

  1. a标签的href="javascript:void(0)"和href="#"的区别

    修正一个说法上的bug吧.对于IE6来说,点击后gif暂停bug仅仅发生在“javascript:伪协议未加分号”的情形下. 我再来提供一个视角吧. 给<a>标签增加href属性,就意味着 ...

  2. Java实战之01Struts2-01简介及环境搭建

    一.Struts2简介 1.Struts2概述 Struts2是Apache发行的MVC开源框架.注意:它只是表现层(MVC)框架. 2.Struts2的来历 Struts1:也是apache开发的一 ...

  3. Spring框架的初步学习

    (1) IOC 控制反转 所谓的控制反转就是应用本身不负责依赖对象的创建和维护,依赖对象的创建及维护是由 外部容器负责的(spring是外部容器之一).这样控制权就由应用转移到了外部容器,控制权 的转 ...

  4. VMWare Workstation 占用443端口导致apache启动不了

    中午安装vm,装linux 系统,搞了好几次才装成功,下午启动apache 忽然发现apache启动不了,各种郁闷啊,打开错误日志,NameVirtualHost无效,各种郁闷呐,试着修改端口,修改配 ...

  5. xml之phpdom操作

    php xml编程XML解析技术介绍 1.php与DOM 2.PHP与XPath 3.SimpleXML DOM(document object model)文档对象模型 把一个文件看做一个对象模型, ...

  6. odoo 清除所有运行数据

    测试odoo,如果需要一个干净的db.经常需要清除掉所有业务数据.做如下操作,较为方便 1:建立一个服务器动作,动作的python代码入下. 然后新建一个菜单,菜单动作关联到 这个动作.需要清空db, ...

  7. POJ 2442 Sequence 优先队列

    题目: http://poj.org/problem?id=2442 #include <stdio.h> #include <string.h> #include <q ...

  8. MyEclipse配置多个WEB容器

    MyEclipse支持多个同版本WEB容器同时运行 打开 然后按下图操作 咱们就得到了 下面需要配置新增加WEB容器的启动路径,在新增加的WEB容器上点击右键,选择箭头指向的菜单 打开的窗口如图,可以 ...

  9. handoff了解

    iOS8推出一个新特性,叫做Handoff.Handoff中文含义为换手(把接力棒传给下一个人),可以在一台Mac和iOS设备上开始工作,中途将工作交换到另一个Mac或iOS设备中进行.这个在iOS8 ...

  10. Tomcat基础教程(四)

    一.将Web应用部署到Tomcat中 为什么要部署?将Web应用部署到Tomcat中,那么Tomcat就能找到相应的Web应用,当Tomcat启动时就会加载和初始化Web应用,而在Tomcat启动后, ...