Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for an Ajax call working on populating a drop down list in Selenium

Ok so I have two drop down lists. List B populates based on the selection made on List A using the Ajax technology.

Now the problem is that once I select an option form List A, I am not able to see the List B populated as Ajax is taking 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 am a beginner so I am sorry if my question sounds stupid. But I am really stuck at this for long.

I can't use:

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

because the id, xpath etc remains the same always, even when the list is not populated.

like image 662
mohit Avatar asked Jul 06 '13 09:07

mohit


People also ask

How wait for AJAX call in Selenium?

Moreover, the JavaScript Executor can be used to wait for an Ajax call. The executeScript method is used to run a JavaScript command in Selenium. The waiting is done till jQuery.

How do you handle AJAX calls in Selenium?

The reliable solution to handle ajax components(as used in my case) is to wait for the element to be visible on the page using waitUntil() API call of webdriver. Otherwise threadsleep() like solution is not at all recommended to handle Ajax. Show activity on this post. I have used this and it itself waits works fine.

How do you get Selenium to wait until the element is present?

Explicit Wait in SeleniumBy using the Explicit Wait command, the WebDriver is directed to wait until a certain condition occurs before proceeding with executing the code. Setting Explicit Wait is important in cases where there are certain elements that naturally take more time to load.

How can we automate interactions with AJAX controls in Selenium?

For that, Selenium Webdriver has to use the wait method on this Ajax Call. So by executing this wait command, selenium will suspend the execution of current Test Case and wait for the expected or new value. When the new value or field appears, the suspended test cases will get executed by Selenium Webdriver. Thread.


1 Answers

I'd suggest two approaches, one is waiting for option Item x, the other way is waiting for options count to be greater than one.

So try the followings (untested Java code, so you might need to debug a bit):

Wait for one option you want (either 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 for options count bigger 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;
  }
});
like image 88
Yi Zeng Avatar answered Sep 19 '22 20:09

Yi Zeng