Wait for an Ajax call to complete with Selenium 2 WebDriver

If you’re using jQuery for your ajax requests, you can wait until the jQuery.active property is zero. Other libraries might have similar options.

public void WaitForAjax()
{
    while (true) // Handle timeout somewhere
    {
        var ajaxIsComplete = (bool)(driver as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0");
        if (ajaxIsComplete)
            break;
        Thread.Sleep(100);
    }
}

Leave a Comment