3

I using Selenium 2 and Java 1.7. I want to wait my HtmlUnitDriver until ajax done when i clicked filter button.

My driver:

Webdriver driver = new HtmlUnitDriver(true);

Filter button and click action:

WebElement weFilterButton = driver.findElement(By.name("filterButton"));
weFilterButton.click();

I tried three ways for wait AJAX done.

first:

WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ctl00_ContentPlaceHolder1_Reports1_ajaxloadingImage"))); 

second:

Boolean el = wait.until(new ExpectedCondition<Boolean>() {
    public Boolean apply(WebDriver driver) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        return (Boolean) js.executeScript("return document.readyState").toString().equals("complete");
    }
});

and although it is not a good solution

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

but is not work.

EDIT

Selenium dependency is :

<dependency>
     <groupId>org.seleniumhq.selenium</groupId>
     <artifactId>selenium-java</artifactId>
     <version>2.45.0</version>
</dependency> 
2
  • The first one is the right one in general, why didn't it work? Commented Jun 28, 2015 at 17:41
  • @tilois ajaxloadingImage can't disabled any element in the page. I clicked filter button over and over. Commented Jun 28, 2015 at 18:26

3 Answers 3

1

Use the code below. It checks if the JQuery is active or not and wait till it is active.

Boolean isJqueryUsed = (Boolean)((JavascriptExecutor)driver).executeScript("return (typeof(jQuery) != 'undefined')"));
if(isJqueryUsed){
  while (true){
    // JavaScript test to verify jQuery is active or not
    Boolean ajaxIsComplete = (Boolean)(((JavascriptExecutor)driver).executeScript("return jQuery.active == 0"));
    if (ajaxIsComplete) break;
    try{
      Thread.sleep(100);
    }catch (InterruptedException e) {}
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

The main problem is after you click something you have to wait for the ajax load to be invisible not visible.That means you have to wait for the element to fade away or hide.

You can achieve that by using not in ExpectedConditions.It will return a boolean type.If it is true you can proceed .Else increase the wait time and see..

 WebDriverWait wait = new WebDriverWait(driver, 60);
   Boolean present = wait.until(ExpectedConditions.not(ExpectedConditions.visibilityOfElementLocated(By.id("ctl00_ContentPlaceHolder1_Reports1_ajaxloadingImage"))));

2 Comments

Hi @Madhan i try invisibilityOfElementLocated which you say the same with what you. but its not working.
Try that in firefox or chrome first and then see...Are you getting any exceptions.And what is the return value you are getting..Are you sure the element is visible[the ajax loader] right after the button click else you have to wait some time before checking the invisibility
0

Give a try with "return Ajax.activeRequestCount == 0"

1 Comment

This is for people using Prototype

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.