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

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

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. java新手笔记10 构造器

    1.摇奖小程序 package com.yfs.javase; import java.io.IOException; import java.nio.CharBuffer; import java. ...

  2. Spring学习之代理

    Spring的核心就是IOC和AOP IOC就是控制反转:   就是用配置文件的方式给javabean 赋值. 正常的在程序里;用new 的方式创建一个对象的时候,他就固定了值, 如果是注入的方式的话 ...

  3. java之泛型潜在错误

    如果使用带泛型声明的类时,没有传入类型参数,那么这个类型参数默认是声明该参数时指定的第一个上限类型,这个类型参数被称为raw type(原始类型 ). eg:     public class Lis ...

  4. ASP.NET中的Request、Response、Server对象

    Request对象 Response.Write(Request.ApplicationPath) //应用根路径 Request.AppRelativeCurrentExecutionFilePat ...

  5. mysql 5.7.16多源复制

    演示一下在MySQL下搭建多主一从的过程. 实验环境: 192.168.24.129:3306 192.168.24.129:3307 192.168.24.129:3308 主库操作 导出数据 分别 ...

  6. linux下多线程踩过的坑(不定更新)

    1,多线程下整个进程的退出 <<APUE>>关于进程环境一章中指出了进程退出的8个条件: ... (4)最后一个线程从启动例程中返回 (5)最后一个线程调用pthread_ex ...

  7. yii2源码学习笔记(二十)

    Widget类是所有部件的基类.yii2\base\Widget.php <?php /** * @link http://www.yiiframework.com/ * @copyright ...

  8. Redis的PHP操作手册(自用)

    String 类型操作 string是redis最基本的类型,而且string类型是二进制安全的.意思是redis的string可以包含任何数据.比如jpg图片或者序列化的对象 $redis-> ...

  9. Oracle课堂实验一“表的使用”代码。

    --创建本地管理表空间CustomerTBSCREATE TABLESPACE CustomerTBS         DATAFILE 'd:\Oracle11\product\11.2.0\ora ...

  10. grub命令来引导linux

    由于对linux系统的好奇,想按在机器上玩玩.昨天忙活了一晚上,最终才把linux安装好.但高兴的有点太早了,我还以为进linux就像进 windows那么简单哪,没有想到却蹦出来一个引导命令(gru ...