Do we have any generic function to check if page has completely loaded in Selenium

前端 未结 5 1218
清歌不尽
清歌不尽 2020-11-21 22:56

I am trying to check if web page is loaded completed or not (i.e. checking that all the control is loaded) in selenium.

I tried below code:

new WebDr         


        
5条回答
  •  逝去的感伤
    2020-11-21 23:26

    Implement this, Its working for many of us including me. It includes Web Page wait on JavaScript, Angular, JQuery if its there.

    If your Application is containing Javascript & JQuery you can write code for only those,

    By define it in single method and you can Call it anywhere:

              // Wait for jQuery to load
              {             
                ExpectedCondition jQueryLoad = driver -> ((Long) ((JavascriptExecutor) driver).executeScript("return jQuery.active") == 0);
    
                boolean jqueryReady = (Boolean) js.executeScript("return jQuery.active==0");
    
                if (!jqueryReady) {
                    // System.out.println("JQuery is NOT Ready!");
                    wait.until(jQueryLoad);
                }
                wait.until(jQueryLoad);
              }
    
              // Wait for ANGULAR to load
              {               
                String angularReadyScript = "return angular.element(document).injector().get('$http').pendingRequests.length === 0";
    
                ExpectedCondition angularLoad = driver -> Boolean.valueOf(((JavascriptExecutor) driver).executeScript(angularReadyScript).toString());
    
                boolean angularReady = Boolean.valueOf(js.executeScript(angularReadyScript).toString());
    
                if (!angularReady) {
                    // System.out.println("ANGULAR is NOT Ready!");
                    wait.until(angularLoad);
                }
              }
    
              // Wait for Javascript to load    
              {             
                ExpectedCondition jsLoad = driver -> ((JavascriptExecutor) driver).executeScript("return document.readyState").toString()
                        .equals("complete");
    
                boolean jsReady = (Boolean) js.executeScript("return document.readyState").toString().equals("complete");
    
                // Wait Javascript until it is Ready!
                if (!jsReady) {
                    // System.out.println("JS in NOT Ready!");
                    wait.until(jsLoad);
                }
              }
    

    Click here for Reference Link

    Let me know if you stuck anywhere by implementing.

    It overcomes the use of Thread or Explicit Wait.

提交回复
热议问题