We can handle complete page load with calling JavascriptExecutor in selenium with java. We can combine explicit wait with JavascriptExecutor so that we can stop execution if page does not loaded with in specific time timeoutInSeconds. Use the following script to wait for complete page load.
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class PageLoadWaiter {
public static void waitForPageToLoad(WebDriver driver, int timeoutInSeconds) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeoutInSeconds));
JavascriptExecutor js = (JavascriptExecutor) driver;
// Wait for the document.readyState to be complete
wait.until((ExpectedCondition<Boolean>) webDriver ->
js.executeScript(“return document.readyState”).toString().equals(“complete”));
}
}