-1

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);
                          }
                  }
        }
aswathy
  • 821
  • 3
  • 15
  • 27
  • Have you tried resetting you elements after encountering that exception? Your retry code doesn't seem to ever do anything about stale elements, it just tries the same thing again and again. – M. Prokhorov Mar 30 '17 at 10:24
  • @M.Prokhorov I am new to this. My objective was to keep looping till the element properly loads, that is when stale element exception comes I guess, ie when element is not loaded properly.Could you please explain. I didn't understand what you said regarding reseting elements – aswathy Mar 30 '17 at 10:36
  • 1
    Stale element reference means that element is no longer part of your page for whatever reason. Continuing to call same method on same element after that will just produce more exceptions - you have to query for element again if you want to use it again. – M. Prokhorov Mar 30 '17 at 10:38
  • @M.Prokhorov could you modify my code and show me.... as an example – aswathy Mar 30 '17 at 10:42
  • What are you actions before shared code? – Kushal Bhalaik Mar 30 '17 at 11:10
  • @kushalツ jst applying some filters. I have modified the code. I am taking data from exel sheet. so for loop with var i is for looping through all rows in the exel sheet – aswathy Mar 30 '17 at 11:20
  • Please take a minute to properly indent and format your code. It's really hard to read as is. Your code references `element1`, `element2`, and `element3` but you never show the locators. Also, please post the relevant HTML and explain the scenario. I'm guessing this can be rewritten more efficiently once we have all the info and the HTML. – JeffC Mar 31 '17 at 03:12
  • I reckon after `ApplyFilters_element().click();` all elements are refreshing, so try to identify `Element 1` and `element 2` in while loop before using them in try-catch – Kushal Bhalaik Mar 31 '17 at 04:38

1 Answers1

1

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!

Xwris Stoixeia
  • 1,831
  • 21
  • 22
  • 4 opening and 4 closing parentheses. This is working code, verified. The isDisplayed() returns true/false and this subsequently is saved in boolean elementPresent – Xwris Stoixeia Mar 30 '17 at 12:10
  • It is working to an extend.... But I am getting "Timed out after 30 seconds waiting for visibility of element located by By.xpath:" as the loop executes 3rd .....loading problem ? – aswathy Mar 30 '17 at 13:27
  • The answer helps but does not completely solve the issue as the 3rd time the loop runs...timeout exception is coming... – aswathy Mar 30 '17 at 13:29
  • 1
    @aswathy You can manipulate the waiting time by changing timeout (i.e. timeout=10 will poll the webElement for 10seconds) before throwing the TimeoutException. This is expected behaviour and it means the element was NOT FOUND in the give time. Maybe you should refresh the page at the end of the loop so you get a fresh element each time? Please try to remove all thread.Sleeps and replace them with the waiting method. Or alternatively try using at the end of the loop: driver.navigate().refresh(); – Xwris Stoixeia Mar 30 '17 at 14:19