0

i have used the below code to check if the page has been loaded:

JavascriptExecutor js = (JavascriptExecutor)Driver;
pageLoadStatus = js.executeScript("return document.readyState").toString();

document.readyState returns "complete" even when the page is still loading visibly.

Pointy
  • 405,095
  • 59
  • 585
  • 614
Ajeeth
  • 19
  • 4
  • A more reliable strategy is to wait on the element (existence or visibility) you're wanting to click or otherwise interact. It takes the guesswork and errors out of your script/code. This way, the page can be partially loaded, and you can still read content or click the element. – Ron Norris Mar 29 '18 at 14:22
  • Possible duplicate of [Selenium IE WebDriver only works while debugging](https://stackoverflow.com/questions/47709234/selenium-ie-webdriver-only-works-while-debugging) – undetected Selenium Mar 29 '18 at 14:28

1 Answers1

0

What you may be seeing are ajax elements working in the background. If I need to know more than just that the page is finished loading, I use the following:

public void waitForJQueryToBeActive() {

    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) {
            }
        }
    }

}
Bill Hileman
  • 2,798
  • 2
  • 17
  • 24