0

How to solve stale element exception I have already tried with web driver wait and try n catch but still getting the same. It is clicking the button also which I am trying to click in a table and also navigating to next page.

This is the code for the same:

for(int cnum=1;cnum<=1;cnum++)  
                {
                System.out.println("SOURCE_TABLE_ID-> " + columns.get(1).getText() + "\t ||" +"\t SOURCE_TABLE_NAME-> "+ columns.get(2).getText());

                String beforexpath = "//*[@id='root']//table/tbody/tr[";
                String afterxpath = "]/td[3]";

                for (int i=1;i<=rnum;i++) 
                {
                    String SourceTableName= driver.findElement(By.xpath(beforexpath+i+afterxpath)).getText();

                    if(SourceTableName.contains("bank_data")) 
                    {
                       WebElement sourcetable_element =new WebDriverWait(driver, 70).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='root']//table/tbody/tr["+i+"]/td[5]/div[@class='glyphicon glyphicon-plus']")));
                        sourcetable_element.click();
                        break;
                        }
                    else
                    {
                        System.out.println("does not starts with B so not clicking");
                    }
vezunchik
  • 3,669
  • 3
  • 16
  • 25

1 Answers1

2

First of all lets be clear about what a WebElement is.

A WebElement is a reference to a specific element in the DOM.

A StaleElementReferenceException is thrown when the element you were interacting with is destroyed and then recreated resulting in your WebElement (the reference to a specific element in the DOM) becoming stale (i.e. it no longer references anything because the element it did reference has been destroyed).

Most complex web pages these days will move things about on the fly as the user interacts with it and this requires elements in the DOM to be destroyed and recreated causing StaleElementReference errors. Remember, the element that has been recreated may look identical to the element that was destroyed, so visually the page may look identical when in reality the original element has been destroyed and then recreated.

The only way you can fix a StaleElementReferenceException is to reset your reference to an element in the DOM, this is done by searching for the element again. So once your WebElement has become stale you need to perform another driver.findElement() to find the element in the DOM again which will reset the reference.

Ardesco
  • 7,281
  • 26
  • 49
  • I have tried the same but still, it's not working – Poonam Shekhawat May 29 '19 at 13:45
  • Which line of code is throwing the error, can you add the entire stack trace to your question – Ardesco May 29 '19 at 15:31
  • Actually, what is happening In a table I am trying to click a plus sign button which is dynamic and with this code, it is happening also but at the same time it is throwing an exception. – Poonam Shekhawat May 30 '19 at 06:13
  • I would still like to know which line of code is throwing the error and I would still like to see the full stack trace to help :) – Ardesco May 30 '19 at 07:40
  • This is the code - WebElement sourcetable_element =new WebDriverWait(driver, 50).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='root']//table/tbody/tr["+i+"]/td[5]/div[@class='glyphicon glyphicon-plus']"))); sourcetable_element.click(); – Poonam Shekhawat May 30 '19 at 08:03
  • So with this code, I am able to click the button but still getting the exception – Poonam Shekhawat May 30 '19 at 08:05