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 5,326 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,950 points
while(staleElement){
  try{
     driver.FindElement(By.XPath(link_click), 10).Click();
     staleElement = false;
  } catch(StaleElementReferenceException e){
    staleElement = true;//might be chance for infinity--coz (if the try-block keep on -failing.it won't resolve the issue,--> My Percepton in one case.)
  }
}

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
3,009 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
15,427 views
0 votes
2 answers

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,520 points
3,997 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
4,949 views
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,890 points
2,892 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2 in Selenium by Sri
• 2,970 points
3,316 views