Regarding waitforPageToLoad(); | Selenium Forum
Y
yedramiarun Posted on 13/12/2022

waitforPageToLoad();---This method is working perfect within the same window and if switching to another window this method is not working. Can u please let me know how the above works perfectly during switching/after switching to windows.

 

public void wait(int time){
try {
Thread.sleep(time*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void waitForPageToLoad(){
JavascriptExecutor js = (JavascriptExecutor)driver;
int i=0;

while(i!=10){
String state = (String)js.executeScript("return document.readyState;");
System.out.println(state);

if(state.equals("complete"))
break;
else
wait(2);

i++;
}
wait(2);// wait of 2 sec between page status and jquery
// check for jquery status
i=0;
while(i!=10){

Boolean result= (Boolean) js.executeScript("return window.jQuery != undefined && jQuery.active == 0;");
System.out.println(result);
if(result )
break;
else
wait(2);
i++;

}

}