0

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?

1 Answers1

0

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

Justin Lambert
  • 940
  • 1
  • 7
  • 13
  • https://stackoverflow.com/questions/12967541/how-to-avoid-staleelementreferenceexception-in-selenium Please check this too – Justin Lambert Jun 19 '20 at 11:16
  • https://www.softwaretestingmaterial.com/stale-element-reference-exception-selenium-webdriver/ – Justin Lambert Jun 19 '20 at 11:17
  • Hi Justin, Thanks for your reply. I already added the While loop in the code but I am getting same error again and again. However, as per your comments I added refresh code..web page shows total 40 account after first click and after refreshing..web page shows again 20 accounts. – Nitin Kapoor Jun 19 '20 at 11:36
  • Hi Nitin please check above link in comments also – Justin Lambert Jun 19 '20 at 11:46
  • Thanks Justin. Information in first link is very helpful. Now it is working after adding Try and catch. – Nitin Kapoor Jun 19 '20 at 12:10