Selenium Ajax wait if Ajax returns no elements? Selenium Ajax wait if Ajax returns no elements? selenium selenium

Selenium Ajax wait if Ajax returns no elements?


Selenium won't wait for AJAX loading. It automatically waits for a page loading. To wait for AJAX type loading you have to use Implicit and Explicit wait.

You can use Implicit Wait and Explicit Wait to wait for a particular Web Element until it appears in the page. The wait period you can define and that is depends upon the application.

Explicit Wait:

An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. If the condition achieved it will terminate the wait and proceed the further steps.

Code:

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

Or

WebElement myDynamicElement = (new WebDriverWait(driver, 30)).until(new ExpectedCondition<WebElement>(){@Overridepublic WebElement apply(WebDriver d) {return d.findElement(By.id("myDynamicElement"));}});

This waits up to 30 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 30 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.

You can use ExpectedConditions class as you need for the application.

Implicit Wait:

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available

Code:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

One thing to keep in mind is that once the implicit wait is set - it will remain for the life of the WebDriver object instance

For more info use this link http://seleniumhq.org/docs/04_webdriver_advanced.jsp

You can use these waits during your AJAX loading.

I hope this will be helpful.