StaleElementReferenceException

StaleElementReferenceException

本文介绍了如何避免“StaleElementReferenceException"在硒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java 实现很多 Selenium 测试.有时,我的测试由于 StaleElementReferenceException 而失败.您能否提出一些使测试更稳定的方法?

I am implementing a lot of Selenium tests using Java. Sometimes, my tests fail due to a StaleElementReferenceException. Could you suggest some approaches to making the tests more stable?

推荐答案

如果页面上发生的 DOM 操作暂时导致元素无法访问,就会发生这种情况.考虑到这些情况,您可以尝试在循环中多次访问该元素,然后最终抛出异常.

This can happen if a DOM operation happening on the page is temporarily causing the element to be inaccessible. To allow for those cases, you can try to access the element several times in a loop before finally throwing an exception.

试试这个来自 darrelgrainger.blogspot.com 的优秀解决方案:

public boolean retryingFindClick(By by) {
    boolean result = false;
    int attempts = 0;
    while(attempts < 2) {
        try {
            driver.findElement(by).click();
            result = true;
            break;
        } catch(StaleElementException e) {
        }
        attempts++;
    }
    return result;
}

这篇关于如何避免“StaleElementReferenceException"在硒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 19:24