加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

等待元素加载的几种处理

发布时间:2020-12-14 14:39:08 所属栏目:百科 来源:网络整理
导读:1 硬性等待 Thread.sleep( int sleeptime); ? 2 智能等待 public void waitForElementToLoad( int timeOut, final By By) { try { ( new WebDriverWait(driver,timeOut)).until( new ExpectedConditionBoolean () { public Boolean apply(WebDriver driver)

1 硬性等待

Thread.sleep(int sleeptime);

?

2 智能等待

public void waitForElementToLoad(int timeOut,final By By) {
    try {
        (new WebDriverWait(driver,timeOut)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                WebElement element = driver.findElement(By);
                return element.isDisplayed();
            }
        });
    } catch (TimeoutException e) {
        Assert.fail("超时!! " + timeOut + " 秒之后还没找到元素 [" + By + "]",e);
    }
}

此方法有两个参数,timeOut是等待元素的超时时间,就是说过了这个时间如果元素还没加载出来就报错。By对象,这个是你元素的定位方式比如By.id(login);

这个方法会在给定timeOut去查找元素,如果在小于timeOut的时间内找到了元素,剩下的时间不在等待,直接执行接下来的操作。

3 设置等待页面加载完毕

有时候我们打开一个网页,网页加载速度比较慢,我们又想等网页完全加载完毕了在执行操作,该怎么办?

int pageLoadTime = 10;

driver.manage().timeouts().pageLoadTimeout(pageLoadTime,TimeUnit.SECONDS);

这段代码,加载driver.get(url)方法之前,他们等待你给定的时间,如果在给定时间内网页还是没有加载出来就会报错,如果在小于给定时间内加载完毕了,剩下的时间不再等待。

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读