java+selenium 时间等待

一、概述

       在我们 开发一脚本时,因为代码的执行是非常快的,但浏览器的反应 往往需要一定时间 才能下一步  这样代码和浏览器不能同步会导致 报错  代码不稳定,为了更接近 用户的方式操作浏览器 我们开发的代码 往往在每一步执行 要等待浏览器反应过来在执行。

时间等待 有三种:强制等待、显示等待、隐式等待

1、强制等待 

就是硬等待,使用方法Thread.sleep(int sleeptime),使用该方法会让当前执行进程暂停一段时间(你设定的暂停时间)。弊端就是,你不能确定元素多久能加载完全,如果两秒元素加载出来了,你用了30秒,造成脚本执行时间上的浪费, 代码如下图所示:

注:Thread.sleep() 单位是毫秒

import drive.DriveBrowser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class ById {


    public static void main(String[] args) throws InterruptedException {

        // 启动浏览器
        WebDriver dr1 = new DriveBrowser().dr();

        // 打开百度
        dr1.get("http://www.baidu.com");

        // id元素定位到输入框 搜索selenium
        dr1.findElement(By.id("kw")).sendKeys("selenium");

        // 等待3秒做下一步操作
        Thread.sleep(3000);



        // 点击百度一下
        dr1.findElement(By.id("su")).click();


    }
}

  

2、显示等待

  由WebDriver 提供 就是明确的要等到某个元素的出现或者是某个元素的可点击等条件等到为止,才会继续执行后续操作,等不到,就一直等,除非在规定的时间之内都没找到,那么就抛出异常了

默认每500毫秒扫描界面是否出现元素、针对单一元素、可以设置超时时间

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.concurrent.TimeUnit;

public class Wait {


    public static void main(String[] args) {

        // 启动浏览器
        WebDriver dr = new DriveBrowser().dr();
        // 打开百度网页
        dr.get("http://wwww.baidu.com");

        // 显示等待,针对某个元素等待
        WebDriverWait wa = new WebDriverWait(dr, 5, 1);
        wa.until(new ExpectedCondition<WebElement>() {
            //重写方法
            @Override
            public WebElement apply(WebDriver text) {
                return text.findElement(By.id("kw"));
            }
        }).sendKeys("selenium");

        // 第二种写法
        //wa.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.id("kw")));

        // 点击百度一下
        dr.findElement(By.id("su")).click();
        // 强制等待3秒
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        dr.quit();


    }

3、隐式等待

WebDriver 提供了三种隐性等待方法:

  • implicitlyWait   识别对象时的超时时间。过了这个时间如果对象还没找到的话就会抛出NoSuchElement 异常。
  • setScriptTimeout    异步脚本的超时时间。WebDriver 可以异步执行脚本,这个是设置异步执行脚本,脚本返回结果的超时时间。
  • pageLoadTimeout     页面加载时的超时时间。因为 WebDriver 会等页面加载完毕再进行后面的操作,所以如果页面超过设置时间依然没有加载完成,那么 WebDriver 就会抛出异常。 

隐式等待(implicit),方法implicitlyWait(long time, TimeUnit.SECONDS) 即全局设置,对整个driver都有作用,如在设定时间内,特定元素没有加载完成,则抛出异常,如果元素加载完成,剩下的时间将不再等待。

 1     //隐式等待
 2     public void implicit(){
 3 
 4         WebDriver dr = new DriveBrowser().dr();
 5         //启动浏览器
 6         dr.get("http://www.baidu.com");
 7 
 8         //页面加载超时时间设置为 5s
 9         dr.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
10         dr.get("https://www.baidu.com/");
11 
12         //定位对象时给 10s 的时间, 如果 10s 内还定位不到则抛出异常
13         dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
14         dr.findElement(By.id("kw")).sendKeys("selenium");
15 
16         //异步脚本的超时时间设置成 3s
17         dr.manage().timeouts().setScriptTimeout(3, TimeUnit.SECONDS);
18 
19         dr.quit();
20 
21     }
原文地址:https://www.cnblogs.com/niunai/p/14355898.html