How to handle Ajax drop down

57 views
Skip to first unread message

Srinvas Dhadavai

unread,
Jun 27, 2017, 10:12:03 AM6/27/17
to Selenium Users
Hi All,

I am designing a script where I have a dependency on dropdowns which is ajax call, I have to wait until the 2nd dropdown values to be refreshed.
Situation is the 2nd drop-down is visible, clickable and selectable so I cannot use visibilityOfElementLocated, elementToBeClickable and elementToBeSelectable.

Could anyone give me suggestions how to wait until drop-down refresh through ajax call?

Please let me know if you need further information.

Thanks,
Srinvas D.

BlackFrost

unread,
Jun 30, 2017, 8:45:08 PM6/30/17
to Selenium Users
Hey,

1) In the first case I assume that you know the option that you want to pick from the 2nd drop down control once AJAX call is complete.
So, the idea of this approach is to keep trying selecting that specific option from the 2nd drop down for a certain amount of time (hoping that withing that time the 2nd dropdown gets the updated list of options and you'll be able to pick your option).
Hope it's not too confusing, but let's have a look at the code that I assume should work :)  :

            SelectElement select = new SelectElement(driver.FindElement(By.XPath("//path_to_drop_down"))); //here we locate the 2nd dropdown

           
WebDriverWait w = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); //creating a wait object assining 10 seconds wait time
       
           
//And now try picking the option from the dropdown by value (e.g. "Value1"). The code will keep for 10 second.
            w
.Until(ctx =>
           
{
               
try
               
{
                   
select.SelectByValue("Value1"); //attempt to pick option by value. If failed => NoSuchElementExeption is thrown (go to catch block then)
                   
return true; //yay, SelectByValue didn't throw an exception. Means it managed to pick the option we were after. Returning 'true' to stop.
               
}
               
catch (NoSuchElementException) //tried to pick option but it wasn't there. Handling exception to prevent it from stopping our test execution.
               
{
                   
return false; //no good, this iteration was not a success. So telling the "Until" method to try again if we still have time left (<10 sec)
               
}
           
});

2) The second situation is when you just want to know if the ajax call that populates 2nd dropdown has finished. That is tricky (async calls are always triky to track) but you may want to look in direction of JavaScript/JQuery methods calls from you test code (more info: https://stackoverflow.com/questions/3709597/wait-until-all-jquery-ajax-requests-are-done).



Reply all
Reply to author
Forward
0 new messages