5

I am sending some keys to some inputfield. When focus is removed from this element, an ajax request is sent to server if this value I entered is valid. If it is, nothing happens, if not an error message occurs. There are a couple of these fields.

When I say:

driver.findElementById(firstId).sendKeys(firstValue);
driver.findElementById(secondId).sendKeys(secondValue);

The second value will not be sent to the second element because there will be a very short ajax request in the mean time. But since the value is ok (firstValue) it will not bring up any text or anything else.

How can I tell Selenium to wait for this ajax to finish? I do not want to use Thread.sleep.

2
  • 1
    You can use selenium Implicit wait and Explicit wait. Commented Jan 24, 2013 at 10:43
  • @Manigandan What is implicit wait and Expilicit wait? Commented Jan 24, 2013 at 10:55

1 Answer 1

7

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>(){
@Override
public 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. So I have to wait for some period of time? Becuase the AJAX in my situation does not make any element visible or invisible..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.