WebDriver_Selenium 元素等待

WebDriver_Selenium 元素等待

WebDriver元素等待有四种方式:

  • 强制等待
  • 隐式等待
  • 显式等待
  • 流利等待
一、强制等待
try {
    //强制等待5s
    Thread.sleep(5000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

强制等待不足之处:

  1. 不灵活
  2. 如果等待时间不足,程序会报错
  3. 如果等待时间过长,自动化测试的效率会非常低
二、隐式等待
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
三、显式等待

By location = By.xpath("")

1. 显式等待

WebElement foo = new WebDriverWait(driver, Duration.ofSeconds(3))
    .until(driver -> driver.findElement(location));
2. 判断某个元素中是否可见并且是enable的,这样的话才叫clickable;
WebElement foo = new WebDriverWait(driver, Duration.ofSeconds(10))
    .until(ExpectedConditions.elementToBeClickable(location));
3. 判断元素是否出现,只要有一个元素出现,就通过。(代表在不代表可见)
WebElement foo = new WebDriverWait(driver, Duration.ofSeconds(10))
    .until(ExpectedConditions.presenceOfElementLocated(location));
4. 判断元素是否出现。
List<WebElement> eleList = new WebDriverWait(driver, timeOutInSeconds)
    .until(ExpectedConditions.presenceOfAllElementsLocatedBy(location));
5. 判断某个元素是否可见. 可见代表元素非隐藏,并且元素宽和高都不等于 0
WebElement foo = new WebDriverWait(driver, Duration.ofSeconds(10))
    .until(ExpectedConditions.visibilityOfElementLocated(location));
四、流利等待
// 等待30秒,等待元素出现在页面上,检查每5秒出现一次。
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
  .withTimeout(Duration.ofSeconds(30))
  .pollingEvery(Duration.ofSeconds(5))
  .ignoring(NoSuchElementException.class);

WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
  public WebElement apply(WebDriver driver) {
    return driver.findElement(location);
  }
});
  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值