0

Currently I am going through a situation. There are three rows in a the body of the table. I have to perform some action on each row if row matches with a text. For this I am getting the size for the the rows and using for loop and checking the condition. When condition are met, I have to perform some action by which the row is getting removed from the webtable which is as per my expectations. Further, I am getting org.openqa.selenium.StaleElementReferenceException: here "totalOrders.get(i).click();" when the loop try to perform action in next row here

here is some part of my code:

By loading = By.xpath("//div[@class='loading-wrap']");
By orders = By.xpath("//tbody/tr"); //this retruns 3 row

public void invoiceAllStockOrder() {

    eu.waitForInvisibilityOfElementLocated(loading, 10);

    List<WebElement> totalOrders = eu.getElements(orders);
    int rowSize =totalOrders.size();
    if(rowSize == 0) {
        System.out.println("No order");
    }

    else {

        for (int i = 0; i < totalOrders.size(); i++) {
            eu.waitForInvisibilityOfElementLocated(loading, 10);

            totalOrders.get(i).click();//getting stale element exception here when i = 1 but there are still 2 rows left
            selectInstockOrders();
            invoiceOrder();
                
        }
    }
}
Suraj
  • 33
  • 5

1 Answers1

0
If the page has Javascript which automatically updates the DOM, 
you should assume a StaleElementException will occur.

Can you try with the below code, I hope this will work for you

public boolean retryingFindClick(By by) {
    boolean result = false;
    int attempts = 0;
    while(attempts < 2) {
        try {
            driver.findElement(by).click();
            result = true;
            break;
        } catch(StaleElementException e) {
        }
        attempts++;
    }
    return result;
}
This will attempt to find and click the element. If the DOM changes 
between the find and click, it will try again.
YaDav MaNish
  • 1,260
  • 2
  • 12
  • 20