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.
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.
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.
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.
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.
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;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With