zoukankan      html  css  js  c++  java
  • Selenium之显示等待WebDriverWait

    一个显式等待就是在继续执行代码之前编码等待定义一个特定条件发生。最糟糕的例子是Thread.sleep(),这设置了一个准确的等待时间。WebDriver提供了一些方便的方法帮助您些代码来等待要求的时间。WebDriverWaitExpectedCondition的结合就是一种实现的方法。

    WebDriver driver = new FirefoxDriver();
    
    driver.get("http://somedomain/url_that_delays_loading");
    
    WebElementmyDynamicElement = (new WebDriverWait(driver, 10))
    
      .until(new ExpectedCondition<WebElement>(){
    
             @Override
    
             public WebElement apply(WebDriver d) {
    
                       returnd.findElement(By.id("myDynamicElement"));
    
             }});




    //
    // 通过判断 title 内容等待搜索页面加载完毕,Timeout 设置10秒
    (new WebDriverWait(driver, 10))
      .until(new ExpectedCondition<Boolean>() {
         @Override

         public Boolean apply(WebDriver d) {

              return d.getTitle().toLowerCase().endsWith("ztree");

        } });


    在抛出TimeoutException之前这会等待最多10秒钟,或者它找到了元素,在0-10秒之间返回。WebDriverWait默认每500毫秒调用ExpectedCondition直到它成功返回。ExpectedCondition类型的成功返回是布尔值true或非null的返回值。

    ExpectedConditions

    有些自动化web浏览器时常用的条件。下面列出的是每个实现。Java恰巧有方便的方法,因此您不需要编写一个ExpectedCondition类自己或为它们创建自己的实用程序。

    l  元素可点击–元素显示并且可用。

    WebDriverWait wait = new WebDriverWait(driver, 10);

    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));

    ExpectedConditions类中包含了一组与定义条件,可用于WebDriverWait。

  • 相关阅读:
    IDEA 中直接连接远程服务器
    浙江大学软件学院2020年保研上机
    PAT甲级近五年题一览表
    浙江大学计算机与软件学院2021年考研复试上机
    浙江大学计算机与软件学院2019年保研上机
    PAT(甲级)2021年春季考试
    PAT(甲级)2020年冬季考试
    PAT(甲级)2020年秋季考试
    PAT(甲级)2020年春季考试
    PAT(甲级)2019年冬季考试
  • 原文地址:https://www.cnblogs.com/itzhazha/p/6593328.html
Copyright © 2011-2022 走看看