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

0 votes

I am getting an error as 

"stale element reference: element is not attached to the page document", when second time column loop is executed (@line: Columns_row.get(column).click();). 

Note: First time column will get clicked and mobile details will get printed on console. 

Please find the below sample script: 

//To locate rows of table. 

    List < WebElement > rows_table = driver.findElements(By.xpath("xpath/tr"));

    //To calculate no of rows In table.

    int rows_count = rows_table.size();

    //Loop will execute till the last row of table.

    for (int row = 0; row < rows_count-25; row++) {

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

    String li= rows_table.get(row).getAttribute("xxx");

    System.out.println(li);

    //To locate columns(cells) of that specific row.

        List < WebElement > Columns_row = rows_table.get(row).findElements(By.xpath("xpath/td[2]//span/a"));

        //To calculate no of columns (cells). In that specific row.

        int columns_count = Columns_row.size();

        //Loop will execute till the last cell of that specific row.

        for (int column = 0; column < columns_count; column++) {

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

        Columns_row.get(column).click();

        String mbl = driver.findElement(By.xpath("xpath/a")).getText();

        System.out.println(mbl);

        driver.findElement(By.xpath("xpath")).click();

       

        }

        System.out.println("-------------------------------------------------- ");

    }

kindly help if there is any solution for this error. 

Sep 7, 2019 in Selenium by AK

edited Sep 9, 2019 by Omkar 2,177 views

1 answer to this question.

0 votes

Hi AK, to resolve this type of error, you can try using this code snippet:

bool staleElement = true; 

while(staleElement){

  try{

     driver.FindElement(By.XPath(link_click), 10).Click();

     staleElement = false;


  } catch(StaleElementReferenceException e){

    staleElement = true;

  }

}
answered Sep 9, 2019 by Abha
• 27,920 points

Related Questions In Selenium

+1 vote
2 answers
+1 vote
0 answers

python selenium error: element is not attached to the page document

I am scraping Banggood, the problem is ...READ MORE

Apr 9 in Selenium by eslam
• 130 points
312 views
+1 vote
2 answers
0 votes
1 answer

To check that the web page is loaded or not using Selenium Web Driver?

The solution is using Implicit Wait which ...READ MORE

answered May 24, 2018 in Selenium by Meci Matt
• 9,460 points
11,243 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,510 points
2,586 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,870 points
2,040 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,870 points
2,137 views