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

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

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. 重新设置MySQL的root密码

    1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 状态下,其他的用户也可以任意地登录 ...

  2. ubuntu 13.10自定义启动顺序

    添加PPA sudo add-apt-repository ppa:danielrichter2007/grub-customizer sudo apt-get update sudo apt-get ...

  3. python相关博客

    入门:http://www.pythontip.com/ Python之禅--大道至简 美胜于丑,显胜于隐,简胜于繁,繁胜于杂,平胜于迭,疏胜于密,读胜于写...名可名, 请常名 http://www ...

  4. 《sed的流艺术之三》-linux命令五分钟系列之二十三

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...

  5. git 401 错误

    错误信息:error: The requested URL returned error: 401 Unauthorized while accessing https://git.oschina.n ...

  6. webbench

    Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能,最多可以模拟3万个并发连接去测 ...

  7. php checkbox复选框值的获取与checkbox默认值输出方法

    php获取 checkbox复选框值的方法,checkbox在php读取值时要用数组形式哦,我们读取这些值用php post获取是以一个array形式哦. php获取 checkbox复选框值的方法 ...

  8. Delphi接口

    program Demo1; { Create Date: 2014-06-29 Author: P.S.M 1.接口Demo1 } {$APPTYPE CONSOLE} uses SysUtils; ...

  9. sublimeformaya

      网上没有找到这样的插件自己造了一个 https://github.com/jonntd/connectionmaya   附件列表

  10. ssh登录docker容器

    ssh登录docker容器1.启动一个docker容器# docker run -t -i ubuntu/ruby:v1 /bin/bash2.然后在容器里,安装openssh-server open ...