Stale Element Exception Webdriver

The scenario is as follows

We have 2 select boxes- 1. For Age Group 2. Topics and

A button FIND.

 We need to select the age grp, and corresponding to the age grp there will be multiple Topics so we need to select one of topics and click on the find button. This will navigate user to respective page. Come back to home page and select the same age grp.

 And then select the next topic for same age grp and so on till we have verified all the pages for 1st age grp.

Then we move to 2nd age grp and corresponding topics for this age grp is verified and so on…

This looks pity straight forward scenario (but this was not for me at least). Where we need to use Select class for both the list boxes and 2 loops to iterate.

But the catch here is we need to use driver.navigate().back() after we have clicked on the Find button to navigate to the Topic page.

And when we use driver.navigate().back() and the loop runs , web driver will throw Stale element exception.

So what is the solution, for beginners this can be a nightmare situation.

I tried: 1. Wait, thinking that may the full page is not loading so the elements are not recognized.

  1. Tried using navigate().refresh() again to reload the page. But this also did not work.

 

To solve this issue, I created functions for both the list boxes (Age Group & Topics) and the script worked for me.

The code structure is like below:

Note forAgeGrp() & forTopic() are the 2 function which did all the magic for me.

 

 

public static int  forAgeGrp(){

           WebElement ageBox = driver.findElement(By.name("dropdown_first"));    

           sel = new Select(ageBox);

           List<WebElement> allAge = sel.getOptions();

           int cntageBox = allAge.size();

           return cntageBox;         

           }

public static int forTopic(){

           WebElement topic = driver.findElement(By.id("edit-dropdown-second"));

           select = new Select(topic);

           List<WebElement> allTopics = select.getOptions();

           int cnttopic = allTopics.size();          

           return cnttopic;         

     }

@Test

     public void testParHead() throws InterruptedException{

           driver = new FirefoxDriver();

           driver.manage().window().maximize();

           driver.get(“URL”);

           driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);

          

           int agcnt = forAgeGrp();

           for(int i = 1 ; i<agcnt;i++){

                sel.selectByIndex(i);

               

                int cnttopic = forTopic();

                for(int j = 1 ; j<cnttopic;j++){

                     System.out.println(j+"j loop");

                     System.out.println("size"+cnttopic);

                     select.selectByIndex(j);                                  driver.findElement(By.name("op")).click();

                     System.out.println("page title "+driver.getTitle());

                     driver.navigate().back();

forAgeGrp();

                     sel.selectByIndex(i);

                     forTopic();

                }

           }

Conclusion is: After returning back to home page using driver.navigate().back()

Call the functions so the list box Webelement is again created and the Webdriver recreates these webelemets to perform desired action. 

Instead of using driver.get method use driver.navigate().to method and provide the URL.

Like
Reply

To view or add a comment, sign in

More articles by Satnam Singh

  • QA #agiletesting Transition.

    Software development process has changed dramatically. And the industry is adopting to: - 1.

    1 Comment

Others also viewed

Explore content categories