Search code examples
javaseleniumstaleobjectstateexception

Stale element exception still persist in spite of 20 retries


I saw one os the posts before regarding stale element exception and used the retry code for handling it. But inspite of keeping the count at 20 , stale element exception still persists. I can see that the element2 is loaded in the webpage being tested .But its still id'd as stale element. The code works in case of element1 sometimes. but never for element2 code:

for (i = 1; i < 7; i++)
        {   
            sServiceID = ExcelUtils.getCellData(i,Constant.Col_ServiceID);
            System.out.println("sServiceID:"+sServiceID);   

            ServiceID_Filter().clear();//function returns element
            ServiceID_Filter().sendKeys(sServiceID);
            BaseClass.driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

            Thread.sleep(3000);
            ApplyFilters_element().click();
            Thread.sleep(3000);


      boolean result = false;
                        int attempts = 0;
                        while(attempts < 20) {
                            System.out.println("inside stale check loop");
                            BaseClass.driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
                            try {
                                if(element1.isDisplayed()||element2.isDisplayed())  //either one of the elements will be loaded
                                {
                                System.out.println("not stale "+Table_widget.ExportButton().isDisplayed());
                                result = true;
                                break;
                                }
                            } catch(StaleElementReferenceException e) {
                                System.out.println("stale at attempt "+attempts);
                            }
                            attempts++;
                        }
    if(result==true)
                  {
                      if(element1.isDisplayed())
                      {
                          element3.click();  
                          System.out.println(" button clicked");
                          Thread.sleep(1000);
                      }
                      else
                          if(element2.isDisplayed())
                          {  element3.click();  
                             System.out.println("No records found"); 
                             Thread.sleep(1000);
                          }
                  }
        }

Solution

  • In my humble opinion the problem is here:

     BaseClass.driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    
                Thread.sleep(3000);
                ApplyFilters_element().click();
                Thread.sleep(3000);
    

    First of all you are using implicit wait plus thread sleep which is a recipe for disaster. This is what is causing your stale elements exceptions, try something like this below:

    public boolean waitForElement(String elementXpath, int timeOut) {
    
        try{                    
        WebDriverWait wait = new WebDriverWait(driver, timeOut); 
        boolean elementPresent=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(elementXpath)).isDisplayed());
    
        System.out.printf("%nElement is present [T/F]..? ")+elementPresent;
        }        
        catch(TimeoutException e1){e1.printStackTrace();elementPresent=false;}          
    
        return elementPresent;   
     }
    

    Best of luck!