【问题标题】:Why is my code not working properly and getting the following error?为什么我的代码无法正常工作并出现以下错误?
【发布时间】:2017-06-14 13:34:14
【问题描述】:

org.openqa.selenium.TimeoutException:预期条件失败: (试了 10 以 500 MILLISECONDS 为间隔的秒数)

这是我的代码:

public static ExpectedCondition<Boolean> waitForTextToChange(final WebElement element, final String currentText) {
    return new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver arg0) {
            return !element.getText().equals(currentText);
        }
    };
}

使用代码:

WebElement element = driver.findElement(...);
String currentText = element.getText();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(waitForTextToChange(element, currentText));

【问题讨论】:

    标签: java selenium-webdriver


    【解决方案1】:

    执行失败,因为命令没有在足够的时间内完成。可能是您需要 1) 延长等待文本出现的时间,或者 2) 您正在查看的元素可能没有以您期望的方式显示文本。您应该调试并查看 element.getText() 返回的内容。您正在使用等于,因此文本必须完全匹配。如果您有任何空格或小写与大写问题,则文本将不匹配。超时的原因有很多。您应该添加一个 try/catch 并使用断点进行调试,以查看代码中到底发生了什么。

    【讨论】:

      【解决方案2】:

      这是超时错误。 使用 try、catch 处理异常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-19
        • 2013-01-09
        • 1970-01-01
        • 2021-02-08
        • 1970-01-01
        • 1970-01-01
        • 2019-06-28
        • 2016-06-14
        相关资源
        最近更新 更多