有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在使用ExpectedConditions时避免StaleElementReferenceException。属性是什么?

我试图验证元素的属性“innerText”是否已更新(例如,从100到50)。为了避免使用睡眠,我正在等待属性设置。但我收到StaleElementReferenceException异常。可能是因为DOM被重新加载了。重要提示:元素是可见的、存在的,如果没有此元素,则为元素。getText()返回旧值。因此测试失败(因为该属性具有旧值)

现在,我忽略了这个例外,但是你能提出一个更好的解决方案吗

public void waitAttributeToBe(WebElement webElement, String text) {
        try {
            wait.until(ExpectedConditions.attributeToBe(webElement,"innerText", text));
        } catch (StaleElementReferenceException exc) {
            exc.printStackTrace();
        }
}

例外情况:

 org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

共 (1) 个答案

  1. # 1 楼答案

    如果有人需要这方面的帮助,我通过使用元素定位器而不是在参数中传递webelement来修复它。这就是为什么我会有StaleElementReferenceException,因为元素正在刷新,而我正在访问旧的元素

    public void waitAttributeToBe(By by, String attribute, String text) {
            wait.until(ExpectedConditions.attributeToBe(by, attribute, text));
        }