3

enter image description here

I am using the below for waiting until the ajax loader is finished loading

var wait = new WebDriverWait(BrowserFactory.Driver, TimeSpan.FromSeconds(60));
wait.Until(d => (bool) (d as IJavaScriptExecutor).ExecuteScript(
            "return (window.jQuery != undefined) && (jQuery.active == 0)"));

But I get

"Failed: OpenQA.Selenium.WebDriverTimeoutException : Timed out after 60 seconds"

Tanya
  • 289
  • 1
  • 3
  • 12
  • Can you share more details of Exception, For which line its causing exception ? – Ishita Shah Aug 13 '18 at 11:40
  • Error is thrown at the line "wait.Until(d => (bool) (d as IJavaScriptExecutor).ExecuteScript( "return (window.jQuery != undefined) && (jQuery.active == 0)"));" – Tanya Aug 13 '18 at 12:01
  • Check this discussion https://stackoverflow.com/questions/50327132/do-we-have-any-generic-funtion-to-check-if-page-has-completely-loaded-in-seleniu – undetected Selenium Aug 13 '18 at 12:11
  • I get the attached piece of code when I select a element from a list and it disappears immediately after the list name has been loaded. – Tanya Aug 13 '18 at 12:57

2 Answers2

3

Probably the easiest way to wait for the loader is to wait for the loader to appear and then disappear rather than waiting for the background page functions. There are probably several elements you can wait for but here's one from the HTML you provided.

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
By loaderLocator = By.CssSelector("img[src$='ajax-loader.gif']");
wait.Until(ExpectedConditions.ElementIsVisible(loaderLocator));
wait.Until(ExpectedConditions.InvisibilityOfElementLocated(loaderLocator));

You want to wait for the loader to appear and then disappear because if the script runs too quickly (or the page is slow) the wait for invisibility may be triggered and succeed before the loader even appears.

JeffC
  • 22,180
  • 5
  • 32
  • 55
0

Try js below:

var result = true; try { result = (typeof jQuery != 'undefined') ? jQuery.active == 0 : true } catch (e) {}; return result;

This is Java code but logic same:

WebDriver driver = WebDriverRunner.getWebDriver();

new WebDriverWait(driver, 20).until(d ->
{
    try{
        return (boolean) ((JavascriptExecutor) d).executeScript("var result = true; try { result = (typeof jQuery != 'undefined') ? jQuery.active == 0 : true } catch (e) {}; return result;"));
    }
    catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }
});
Sers
  • 12,047
  • 2
  • 12
  • 31
  • 'Failed: System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.' error while using the above code. – Tanya Aug 13 '18 at 11:56
  • Check my answer, I added java example but logic I think is the same. – Sers Aug 13 '18 at 12:47