显式等待-----Selenium快速入门(十)

摘要:
等待<Clock是一个接口:Clock参数用于定义超时。有关更详细的时钟接口。

  上一篇说了元素定位过程中的隐式等待,今天我们来探讨一下显示等待。显式等待,其实就是在使用WebDriverWait这个对象,进行等待。显式等待对比隐式等待,多了一些人性化的设置,可以说是更细化的隐式等待。

  WebDriverWait 类继承自泛型类 FluentWait<T> ,而这个泛型类,又是泛型接口 Wait<T> 的实现。Wait<T>这个泛型接口只有一个方法,就是until,这也是我们需要重点掌握的方法,而FluentWait<T>实现了until方法,而且还扩充了一些设置,例如,设置等待时间,每隔多少时间执行一次,执行过程中哪些异常可以忽略,以及超时的时候显示的信息。同样,WebDriverWait既然继承了FluentWait<T>,也会拥有一样的方法。

  首先,我们如何得到WebDriverWait的实例呢,他的构造函数有3个。

  1.WebDriverWait(WebDriver driver, Clock clock, Sleeper sleeper, long timeOutInSeconds, long sleepTimeOut) 

     driver:这个不用说,大家都应该知道。

     clock:Clock是一个接口,clock参数是用来定义超时,一般使用默认即可。关于更详细的Clock接口,可以点击这里

          sleeper:Sleeper也是一个接口,sleeper这个参数是,让当前线程进入睡眠的一个对象

     timeOutInSeconds:超时的时间,单位是秒

     sleepTimeOut:线程睡眠的时间,默认是500毫秒,也就是默认500毫秒执行一次

  2.WebDriverWait(WebDriver driver, long timeOutInSeconds)

       3.WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis)

  有了第一个构造函数的解释,后面两个就很简单了,实际上,后面两个构造函数更常用。

  由于Clcok和Sleeper都是接口,我们可以自己重新实现,也可以用已有的实现,例如,SystemClock就实现了Clock,而Sleeper,他有一个static final的字段SYSTEM_SLEEPER,我们可以使用它来作为参数。下面是Sleeper接口的源码

public interface Sleeper {

  public static final Sleeper SYSTEM_SLEEPER = new Sleeper() {
    public void sleep(Duration duration) throws InterruptedException {
      Thread.sleep(duration.in(TimeUnit.MILLISECONDS));
    }
  };

  /**
   * Sleeps for the specified duration of time.
   *
   * @param duration How long to sleep.
   * @throws InterruptedException If the thread is interrupted while sleeping.
   */
  void sleep(Duration duration) throws InterruptedException;
}

  所以,要使用构造函数一得到wait对象,可以这样

WebDriverWait wait=new WebDriverWait(driver, new SystemClock(), Sleeper.SYSTEM_SLEEPER, 10, 1000);

  实际上,当我们使用后面两个构造函数的时候,其实也是在默认使用这两个参数,我们看看WebDriverWait构造函数的源码

 /**
   * Wait will ignore instances of NotFoundException that are encountered (thrown) by default in
   * the 'until' condition, and immediately propagate all others.  You can add more to the ignore
   * list by calling ignoring(exceptions to add).
   *
   * @param driver The WebDriver instance to pass to the expected conditions
   * @param timeOutInSeconds The timeout in seconds when an expectation is called
   * @param sleepInMillis The duration in milliseconds to sleep between polls.
   * @see WebDriverWait#ignoring(java.lang.Class)
   */
  public WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis) {
    this(driver, new SystemClock(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, sleepInMillis);
  }

  懂得第一个构造函数的使用,后面两个就不啰嗦了,后面两个实际是在使用默认的参数调用第一个构造函数。

  现在我们得到了WebDriverWait对象,怎么实现等待呢。刚说了,until是当中最重要的一个方法,我们看看WebDriverWait中这个until方法的定义。

  public <V> V until(java.util.function.Function<? super WebDriver,V> isTrue) ,

  until方法中的参数是一个Function,其中? super WebDriver是参数,表示包括WebDriver在内的所有WebDriver的父类,V是返回值的类型。

  例如:

     //得到WebDriver
        WebDriver driver=DriverHelper.CreateChromeDriver();
        
        //定义超时10秒,默认每500毫秒轮询一次
        WebDriverWait wait=new WebDriverWait(driver,10);
        //重新定义轮询时间,每隔1秒轮询一次
        wait.pollingEvery(1000, TimeUnit.MILLISECONDS);
        //忽略NoSuchElementException异常
        wait.ignoring(NoSuchElementException.class);
        
        //等待10秒,每隔1秒定位一次,直至超时或返回要找的元素
        WebElement ele = wait.until(new Function<WebDriver,WebElement>() {
            @Override
            public WebElement apply(WebDriver arg0) {
                // TODO Auto-generated method stub
                return arg0.findElement(By.id("eleID"));
            }
            
        });

  until的参数太复杂,搞不懂?没关系,官方早已定义了常见的条件。这些条件都在ExpectedConditions这个类中。里面的方法很多,看名字基本就能猜到用途,官方的文档为:http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html

  例如:

    //得到WebDriver
        WebDriver driver=DriverHelper.CreateChromeDriver();
        
        //定义超时10秒,默认每500毫秒轮询一次
        WebDriverWait wait=new WebDriverWait(driver,10);
       
        //等待直到标题包含abc
        wait.until(ExpectedConditions.titleContains("abc"));
        
        //等待直到元素可点击
        wait.until(ExpectedConditions.elementToBeClickable(By.id("aaa")));
        
        //等待直到url包含cnblogs.com
        wait.until(ExpectedConditions.urlContains("cnblogs.com"));

  更多的预置条件请查看:http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html

  下面通过一个例子,来看看具体怎么使用。

  首先,我们在html文件夹新建一个wait.html,html代码如下:

<!DOCTYPE html>
<html>
<head> 
<meta> 
<title></title> 
</head>
<body>

<input type="text" id="inputID" style="display:none"/>
<input type="button" value="点击" onclick="document.getElementById('inputID').style.display='';"/>

</body>
</html>

  然后,我们的代码要实现的功能是,等待5秒,直到inputID这个文本框显示,我们就在文本框内输入“hello,selenium",html中有个按钮,就是用来控制文本框的显示隐藏状态。要实现这个功能,我们需要用到visibilityOfElementLocated(By located)。

  代码如下:

     //得到WebDriver
        WebDriver driver=DriverHelper.CreateChromeDriver();
        //跳转到html
        driver.get("file:///D:/WorkSpace/SeleniumTest/html/wait.html");

        //等待5秒,如果inputID显示,则输入"hello,selenium",否则,输出信息“定位元素超时”
        WebDriverWait wait=new WebDriverWait(driver, 5);
        try
        {
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("inputID"))).sendKeys("hello,selenium");
        }
        catch (org.openqa.selenium.TimeoutException e) 
        {
            System.out.print("定位元素超时!");
        }
       

  执行效果如下:

  显式等待-----Selenium快速入门(十)第1张

免责声明:文章转载自《显式等待-----Selenium快速入门(十)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇VC6.0设置选项解读(转)我是如何考过阿里云ACP云计算工程师认证的下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

text-align:justify 失效问题。

text-align:justify 失效问题。 <div class="fmlist_left"> <p> <span> 品名 <i class="forjustify"></i> </span>...

Ubuntu中查找文件

首先打开Ubuntu的终端,利用find命令,然后后面跟一个name参数,冒号里式过滤条件,两个星号表示包含name的文件 find / -name "*name" 权限不够,在前面加sudo 只搜索名称为name的文件: find / -name "name" 按照size大小查找文件: find / -size 1G...

easyui datagrid设置fit: true后,页面显示不全的情况

跟工具栏有关 <div id="tb"> <div style="float:left;"> <a href="http://t.zoukankan.com/cuizhf-p-5240229.html#" class="easyui-linkbutto...

数据库-求候选关键字

这类题目都是给定关系模型,求候选关键字.  题型: 这种给定关系模式和函数依赖的题目 ,做法大致有三步: 1、根据关系模式和函数依赖画出有向图. 2、找出是否有入度为0(即是没有任何元素可以推出他的元素),然后尝试是否可以从这个元素开始,随着箭头来遍历这个图,看看是否是哪一个元素都能遍历的到, 如果能够遍历的到的话,就可以判断这个关系的候选关键字为这个...

tomcat设置gzip

使用tomcat发布3dtiles或terrain数据遇到的gzip问题 问题一 对大于1KB小于48KB的json请求进行gzip压缩,json为原文件(对于大于48KB使用sendfile) 1.创建原始文件 2.设置 在apache-tomcat-8.5.9/conf/server.xml文件中,修改节点如下: <Connector port...

Httpd服务入门知识-https(http over ssl)安全配置

         Httpd服务入门知识-https(http over ssl)安全配置                                                 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任。 一.SSL会话的简化过程   (1)客户端发送可供选择的加密方式,并向服务器请求证书   (2)服务器端...