出现错误 - org.openqa.selenium.StaleElementReferenceException:过时的元素参考:元素未附加到页面文档

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

我在网页上做分页。其中 20 个帐户已加载到页面上,再次单击按钮后,又加载了 20 个帐户。第三次单击按钮从网页上消失后,所有帐户都已加载。但我收到以下错误:

"org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached 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();

请大家帮我解决以上问题?

陈旧元素异常意味着元素在网页上,但 selenium 无法与该元素交互,有 2 种方法可以解决该问题 1.Try 带页面刷新(driver.navigate().refresh();) 2.Just 使用循环元素直到单击该元素