0

I have two different drop downs, first drop down consist of 5 values and second one has some ten values, my question is i want to iterate my drop downs in such away that for the each value in first drop down all the options should be verified, i have written the code as below, but after iterating two times it is giving "staleElementReferenceException ".. pls help me out.

public static void data() throws Exception {
    dept = TestNGUtils.getWebDriver().findElement(By.xpath("//*[@id='dept']"));
    Select deptdropbox = new Select(dept);
    listb = deptdropbox.getOptions();
    for (int s = 0; s < listb.size(); s++) {
        System.out.println(listb.get(s).getText());
        listb.get(s).click();
        Thread.sleep(2000);
        region = TestNGUtils.getWebDriver().findElement(By.xpath("//*[@id='SVC_UNIT_NAME']"));

        Select regiondropbox = new Select(region);
        regionlist = regiondropbox.getOptions();

        for (int d = 0; d < regionlist.size(); d++) {
            System.out.println(regionlist.get(d).getText());
            String DD2 = regionlist.get(d).getText();
            regionlist.get(d).click();
        }
    }
}
alexrnov
  • 2,346
  • 3
  • 18
  • 34
  • It seems after complete iteration of second for loop..when it goes back to first for loop..not able to locate element....because DOM operation happening on the page is temporarily causing the element to be inaccessible. Please mention full code snippet..possibly able to help with that. – Sodium Jan 24 '18 at 13:20
  • Hello Gaurav..you are correct,,,, what eve explanation u gave me was crct.. but i wanna get rid of this exception..what has to be done for that.. – Tadimarriakhilabegum Tadimarri Jan 24 '18 at 13:34
  • duplicate to https://stackoverflow.com/questions/48046765/stale-element-reference-error-in-selenium, you can take the solution from Jeffc, his solution more simple. If you can't get the point, tell me, I can modify your code to resolve our problem. I guess when you select different option, it trigger some DOM node changed, which lead to staleElementReferenceException – yong Jan 24 '18 at 13:37
  • Yes Yong..my code is similar to the solution provided...it is successfully iterating through the second loop and going to second value in the first drop down and then saying that stale element,,,its DOM node is getting changed... i can clearly understand the exception but i dont to get rid of it.. :P – Tadimarriakhilabegum Tadimarri Jan 24 '18 at 14:58

3 Answers3

0

The below code may help you,

public static void data() throws Exception
{
    dept = TestNGUtils.getWebDriver().findElement(By.xpath("//*[@id='dept']"));
    Select deptdropbox = new Select(dept);
    listb= deptdropbox.getOptions();
    for ( int s=0; s<listb.size(); s++)
   {
        deptdropbox.selectByIndex(s);
        System.out.println(deptdropbox.getFirstSelectedOption().getText());
        Thread.sleep(2000);
        region = TestNGUtils.getWebDriver().findElement(By.xpath("//*[@id='SVC_UNIT_NAME']"));

        Select regiondropbox = new Select(region);
        regionlist = regiondropbox.getOptions();

       for( int d=0; d<regionlist.size();d++)
       {
            regionlist.selectByIndex(d);
            System.out.println(regionlist.getFirstSelectedOption().getText());
       }
   }
}
Murthi
  • 5,299
  • 1
  • 10
  • 15
0

You should spend some time reading up on what a StaleElementException is, why it is caused, and how to fix it. You will likely run into a situation like this again.

The issue (apparently) is that the first SELECT is getting changed at some point which means that any variable you have pointing to it will throw the exception when you try to reference it. In your case, what you need to do is to get a new reference to the first dropdown before you return to the top of the loop. I tried to simplify things a bit.

public static void data() throws Exception
{
    By deptLocator = By.id("dept");
    By regionLocator = By.id("SVC_UNIT_NAME");
    WebElement dept = TestNGUtils.getWebDriver().findElement(deptLocator);
    List<WebElement> listb = new Select(dept).getOptions();
    for (int s = 1; s < listb.size(); s++)
    {
        // grab a reference to dropdown 2 so we can wait for it to go stale later
        WebElement region = TestNGUtils.getWebDriver().findElement(regionLocator);
        System.out.println(listb.get(s).getText());
        listb.get(s).click();

        // wait for the reference to dropdown 2 to go stale as the result of making a selection in dropdown 1
        new WebDriverWait(driver, 10).until(ExpectedConditions.stalenessOf(region));
        region = TestNGUtils.getWebDriver().findElement(regionLocator); // now that it's stale, get it again
        List<WebElement> regionlist = new Select(region).getOptions();

        for (int d = 0; d < regionlist.size(); d++)
        {
            System.out.println(regionlist.get(d).getText());
            regionlist.get(d).click();
        }

        // get a reference to dropdown 1 again since it's now stale
        dept = TestNGUtils.getWebDriver().findElement(deptLocator);
        listb = new Select(dept).getOptions();
    }
}
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Hey Jeff.. i tried this way also.. unfortunately this is also not working.... Its giving time out exception for me at the line: new WebDriverWait(driver, 10).until(ExpectedConditions.stalenessOf(region)); .... pls look into the same – Tadimarriakhilabegum Tadimarri Jan 30 '18 at 10:03
  • It's impossible to test this since I don't have access to the page. Just comment that line out, now does it work? – JeffC Jan 30 '18 at 14:10
0

@tadimarriakhilabegum-tadimarri below code should work...but it's not optimized way of doing this.

public static void data() throws Exception
    {
        dept = TestNGUtils.getWebDriver().findElement(By.xpath("//*[@id='dept']"));
        Select deptdropbox = new Select(dept);
        listb= deptdropbox.getOptions();

        for ( int s=0; s<listb.size(); s++){

        Select deptdropbox1 = new Select(dept);
            listc= deptdropbox1.getOptions();

            System.out.println(listc.get(s).getText());
            listc.get(s).click();
            Thread.sleep(2000);
            region = TestNGUtils.getWebDriver().findElement(By.xpath("//*[@id='SVC_UNIT_NAME']"));

            Select regiondropbox = new Select(region);
            regionlist = regiondropbox.getOptions();

            for( int d=0; d<regionlist.size();d++){

                System.out.println(regionlist.get(d).getText());
                String  DD2 = regionlist.get(d).getText();
                regionlist.get(d).click();
           }
       }
   }
Sodium
  • 1,016
  • 1
  • 9
  • 22
  • Hello Gaurav, tried the approach suggested by you... now i think outer loop is working fine.. getting staleness at this line: "System.out.println(regionlist.get(d).getText()); "... pls suggest – Tadimarriakhilabegum Tadimarri Jan 29 '18 at 10:31
  • Print list in inner for loop and verify it's content...and further add `Thread.sleep(2000);` after last line in inner for loop. – Sodium Jan 29 '18 at 11:09