zoukankan      html  css  js  c++  java
  • 【Selenium】显示、隐式等待

    显示等待

    WebDriverWait

    超时抛出TimeOutException,默认500毫秒

    public class WaitToReturnElement {

    /*
    * 设置超时时间为5秒,返回指定xpath的WebElement
    * */
    public static WebElement waitForByXpath(final WebDriver driver,final String xpath) {
    WebDriverWait wait = new WebDriverWait(driver, 5);
    return wait.until(new ExpectedCondition<WebElement>() {

    public WebElement apply(WebDriver arg0) {
    return driver.findElement(By.xpath(xpath));
    }

    });
    }

    /*
    * 设置超时时间为10秒,返回指定id的WebElement
    * */
    public static WebElement waitForById(final WebDriver driver,final String id) {
    WebDriverWait wait = new WebDriverWait(driver, 10);
    return wait.until(new ExpectedCondition<WebElement>() {

    public WebElement apply(WebDriver arg0) {
    return driver.findElement(By.id(id));
    }

    });
    }

    /*
    * 设置超时时间为10秒,返回指定xpath的WebElement是否出现
    * */
    public static Boolean isElementDisplayed(final WebDriver driver,final String xpath) {
    WebDriverWait wait = new WebDriverWait(driver, 10);
    return wait.until(new ExpectedCondition<Boolean>() {

    public Boolean apply(WebDriver arg0) {
    return driver.findElement(By.xpath(xpath)).isDisplayed();
    }
    });
    }

    }

    ExpectedCondition

    等待元素直到可点击状态
    WebDriverWait wait=new WebDriverWait(driver,10);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));

    隐式等待

    查找WebDriver无法使用的元素时等待,默认0,生命周期整个WebDriver

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  • 相关阅读:
    人脸识别的一些网站
    41、过滤驱动程序
    13、ActiveX控件
    42、驱动程序调试
    20、宽字符串与字符串间的相互转换
    14、HOOK和数据库访问
    43、Windows驱动程序模型笔记(一)
    7、注册表读写的一个例子
    12、动态链接库,dll
    40、总结IRP,handling IRPs,Part II
  • 原文地址:https://www.cnblogs.com/baoyu7yi/p/7119187.html
Copyright © 2011-2022 走看看