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 8, 2019 in Selenium by AK

edited Sep 9, 2019 by Omkar 11,155 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
• 28,140 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 10, 2020 in Selenium by eslam
• 130 points
10,024 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
25,141 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,500 points
13,673 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,142 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,910 points
7,221 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 3, 2020 in Selenium by Sri
• 3,190 points
7,874 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP