Search code examples
javaselenium-webdrivercucumbercucumber-junit

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


I am doing pagination on the web page. In which 20 account has been load on the page and after clicking the button again another 20 account has been loaded. After third click button disappeared from the web page and all accounts has been loaded. But I am getting below error:

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

Below is the code:

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();

Can you all please help me regarding above?


Solution

  • Stale element exception means element is there on web page but selenium could not interct that element , there are 2 ways to resolve that 1.Try with page refresh(driver.navigate().refresh();) 2.Just use loop element until click that element