首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在使用ExpectedConditions.attributeToBe时如何避免StaleElementReferenceException?

问在使用ExpectedConditions.attributeToBe时如何避免StaleElementReferenceException?
EN

Stack Overflow用户
提问于 2020-09-16 15:17:26
回答 1查看 43关注 0票数 0

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

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

代码语言:javascript
复制
public void waitAttributeToBe(WebElement webElement, String text) {
        try {
            wait.until(ExpectedConditions.attributeToBe(webElement,"innerText", text));
        } catch (StaleElementReferenceException exc) {
            exc.printStackTrace();
        }
}

例外:

代码语言:javascript
复制
 org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-28 22:17:46

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

代码语言:javascript
复制
public void waitAttributeToBe(By by, String attribute, String text) {
        wait.until(ExpectedConditions.attributeToBe(by, attribute, text));
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63914977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档