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。

  • 相关阅读:
    MySQL错误 1030-Got error 28 from storage engine
    电脑开机无反应 不显示BIOS 硬件没问题
    python错误 import: unable to open X server
    Python 错误 invalid command 'bdist_wheel' & outside environment /usr
    Cento 7安装 Failed to execute /init
    笔记《鸟哥的Linux私房菜》5 首次登入与在线求助 man
    Scrapy XPath语法
    Linux 用户操作
    Mysql 表修改
    Ubuntu 配置 Python环境 IPython
  • 原文地址:https://www.cnblogs.com/itzhazha/p/6593328.html
Copyright © 2011-2022 走看看