【问题标题】:Getting error - org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document出现错误 - org.openqa.selenium.StaleElementReferenceException:过时的元素引用:元素未附加到页面文档
【发布时间】:2020-06-19 10:56:13
【问题描述】:

我正在网页上进行分页。其中 20 个帐户已加载到页面上,再次单击按钮后,另一个 20 个帐户已加载。第三次点击按钮从网页上消失后,所有帐户都已加载。但我得到以下错误:

“org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attach to the page document”

下面是代码:

public static WebDriver driver;
public static List<WebElement> tarif;
public static Actions action;
public static boolean flag;

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    tarif = driver.findElements(By.xpath("//button[contains(text(),'tarife')]"));


    flag = true;

        while(flag) {

            System.out.println(flag);

            System.out.println(tarif.size());

            if (tarif.size() > 0) {

                //try {

                    Actions action = new Actions(driver);
                    action.moveToElement(tarif.get(0)).build().perform();
                    Thread.sleep(5000);

                    tarif.get(0).click();

            } else {

                flag = false;
            }

        }

    Thread.sleep(5000);

    driver.quit();

你们能帮我解决以上问题吗?

【问题讨论】:

    标签: java selenium-webdriver cucumber cucumber-junit


    【解决方案1】:

    过时元素异常意味着网页上有元素但硒无法干扰该元素,有两种方法可以解决该问题 1.尝试页面刷新(driver.navigate().refresh();) 2.只需使用循环元素,直到单击该元素

    【讨论】:

    • 嗨贾斯汀,感谢您的回复。我已经在代码中添加了 While 循环,但我一次又一次地遇到相同的错误。但是,根据您的 cmets,我添加了刷新代码..网页在第一次点击后显示总共 40 个帐户,刷新后..网页再次显示 20 个帐户。
    • 嗨 Nitin,请在 cmets 中查看上述链接
    • 谢谢贾斯汀。第一个链接中的信息非常有帮助。现在它在添加 Try and catch 后可以工作了。
    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 2021-12-15
    • 2021-11-25
    • 1970-01-01
    相关资源
    最近更新 更多