如何制作selenium 3.4.0等待页面加载?

时间:2018-05-28 12:20:17

标签: selenium-webdriver wait

我正在使用selenium web驱动程序3.4.0来查找网站的响应时间。在早期版本中,我已经使用过 WebDriver wait = new WebDriverWait(driver,10); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(" MYID&#34)));找到加载的页面。

但这两行代码不适用于3.4.0版本。有没有其他方法来计算页面的加载时间?或者等到页面加载?此外,我需要检测将在按钮单击时加载的模态对话框。 我正在eclipse IDE中使用动态Web项目实现。

我还需要等待一段时间,直到某些点击事件完成加载Dom元素。 Wait.until()不适用于selenium 3.4.0。如何让selenium等到某个元素可见?

1 个答案:

答案 0 :(得分:0)

您可以使用JavascriptExecutor获取页面的就绪状态,并将其作为预期条件传递给您的wait.until()

以下是此参考代码

ExpectedCondition<Boolean> expectation = new
            ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver driver) {
            return ((JavascriptExecutor) driver).executeScript("return document.readyState").toString().equals("complete");
        }
    };
    try {
        Thread.sleep(1000);
        WebDriverWait wait = new WebDriverWait(LocalDriverManager.getDriver(), 30);
        wait.until(expectation);
    } catch (Throwable error) {
        Assert.fail("Timeout waiting for Page Load Request to complete.");
    }