I have been using selenium to test an AngularJS application and I am encountering an issue where I am unable to perform any actions on the page until it is fully loaded. Although I have considered using Thread.sleep(), I am aware that this is not the most efficient solution. I have researched and tried multiple methods to wait for the page to load, but none have been successful so far. Once I navigate to a webpage in my application, it appears visually loaded, yet I still cannot interact with it until it has fully loaded (which takes about 1 second).
If anyone could share their implementation of how they successfully solved this issue, I would greatly appreciate it.
Here is a snippet of my code:
public By ngWait(final By by) {
return new FluentBy() {
@Override
public void beforeFindElement(WebDriver driver) {
driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
((JavascriptExecutor) driver).executeAsyncScript("var callback = arguments[arguments.length - 1];" +
"angular.element(document.body).injector().get('$browser').notifyWhenNoOutstandingRequests(callback);");
super.beforeFindElement(driver);
}
@Override
public List<WebElement> findElements(SearchContext context) {
return by.findElements(context);
}
@Override
public WebElement findElement(SearchContext context) {
return by.findElement(context);
}
@Override
public String toString() {
return "ngWait(" + by.toString() + ")";
}
};
}