How to wait for an Ajax call working on populating a dropdown in Selenium

So, I have two dropdowns. List B is populated based on the choices made in list A using Ajax technology.

Now the problem is that as soon as I select the List A form option, I can’t see the List B populating because Ajax takes a lot of time to load. I want to know how to use the wait condition in this scenario to give Ajax enough time to load. I'm new, so I'm sorry if my question sounds stupid. But I was really stuck with this for a long time.

I can not use:

WebDriverWait wait = new WebDriverWait(driver,30); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id/xpath))); 

because id, xpath , etc. always remain unchanged, even if the list is not populated.

+8
selenium selenium-webdriver
source share
4 answers

I would suggest two approaches: one is waiting for the Item x option, the other is expecting the number of options to be more than one.

So, try the following (unverified Java code, so you might need to debug a little):

Wait until you want (by its value or text):

 By byValue = By.cssSelector("#alertSubCatSelectBox > option[value='18222216517']"); //By byText = By.xpath("//select[@id='alertSubCatSelectBox']/option[text()='Item x']"); new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(byValue)); 

Or wait while the number of options is more than one

 WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(new ExpectedCondition<Boolean>() { public Boolean hasMoreThanOneOptions(WebDriver driver) { return driver.findElements(By.cssSelector("#alertSubCatSelectBox option")).size() > 1; } }); 
+8
source share

There is an onchange call for populatesubcategory fn. This fn shud performs a second list load. Can you post the code for this function, if available. A wait can be made based on this.

+1
source share
 public static void AvoidDropDownDelay(this IWebDriver driver, IWebElement element) { var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); wait.Until(d => { try { if (element.Displayed) return true; } catch (NoSuchElementException) { return false; } return false; }); var randomOption = new Random(); wait.Until(d => element.FindElements(By.TagName("option")).Count > 1); const int startOption = 1; var endOption = element.FindElements(By.TagName("option")).Count; var randomNumber = startOption + randomOption.Next(endOption - startOption); var select = new SelectElement(element); select.SelectByIndex(randomNumber); } 
0
source share

using jquery to test inactive ajax calls (C #)

  public void WaitForAjaxComplete(int maxSecondsToWait = 20) { var maxHundrethSeconds = maxSecondsToWait * 10; for (int i = 1; i <= maxHundrethSeconds; i++) { bool isAjaxCompete = false; isAjaxCompete = (bool)((IJavaScriptExecutor)driver).ExecuteScript("return window.jQuery != undefined && jQuery.active == 0"); if (isAjaxCompete) { return; } System.Threading.Thread.Sleep(100); } throw new Exception("Timed out after " + maxHundrethSeconds + " seconds"); } 
0
source share

All Articles