0

I am looking for function in Java +selenium where I can check or verify if page is fully loaded. I saw onLoad() of JS but nothing on java, is there is something for JAVA?

also I saw these:

WebDriver driver = new AnyDriverYouWant();
if (driver instanceof JavascriptExecutor) {
   ((JavascriptExecutor)driver).executeScript("yourScript();");
} else {
   throw new IllegalStateException("This driver does not support JavaScript!");
}

but again JS and need to write script in JS How to use JavaScript with Selenium WebDriver Java

update - also can try these solution too: void waitForLoad(WebDriver driver) { new WebDriverWait(driver, 30).until((ExpectedCondition<Boolean>) wd -> ((JavascriptExecutor) wd).executeScript("return document.readyState").equals("complete")); }

from here https://stackoverflow.com/a/15124562/12115696

nuzo
  • 49
  • 1
  • 7

1 Answers1

0

In order to determine if a page is fully loaded, you will need to identify which WebElements on the page indicate a loading status. For example, if there is a load mask of some type, you will want to wait until the load mask is hidden to verify that the page is fully loaded.

Here's a simple "Wait until loaded" function that utilizes the ExpectedConditions class:

Given the following HTML for a load mask:

<div id='load-mask' style='display: block'/>

You can use the following code to wait until the load mask is hidden:

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(seconds));
wait.Until(ExpectedConditions.InvisibilityOfElementLocated(By.Id("load-mask")));

Edit -- added a JavaScript-only wait function, as requested by the asker:

 wait.until(driver=> ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete"));

This checks the document.readyState attribute in JavaScript, and completes the wait once readyState is set to complete.

CEH
  • 5,701
  • 2
  • 16
  • 40
  • Thank you, the solution is well known and it's very good but these is first of all in JS and i need in Java (yes i know its very simmilar) but rather than these, i need solution that it won't be depend on a particular element cause i have no such element, and these is the question if there is something like these? any kind of verification that the page is fully loaded on all requests for example – nuzo Sep 25 '19 at 18:22
  • Thank you for the extra details. I have added something to my answer that only checks the JavaScript on the page instead of looking for any elements. – CEH Sep 25 '19 at 18:27
  • Thank you for the edit, but these is working in JS enviroment isn't it? i'm writing in Java, can these solution be edited for Java? – nuzo Sep 25 '19 at 20:43
  • This is Java code that I posted. You can use a Selenium WebDriver to execute JavaScript on the page and check if it is loaded. – CEH Sep 25 '19 at 20:50
  • strange, i tried even to use these before : `JavascriptExecutor js = (JavascriptExecutor) driver;` but still i get error on that line that: `JavaScriptExecutor cannot be resolved to a type` – nuzo Sep 25 '19 at 20:59
  • you have to import JavascriptExecuter class from java API: `import org.openqa.selenium.JavascriptExecutor;` Make sure the s in Javascript is lowercase too. – CEH Sep 25 '19 at 21:02
  • I'm not sure how to help, I can only direct you to people with similar issue: https://stackoverflow.com/questions/24098520/javascript-executor-in-selenium-webdriver/24101994 https://stackoverflow.com/questions/45952444/javascriptexecutor-cannot-be-resolved-to-a-type – CEH Sep 25 '19 at 21:06
  • when its with small s so the error is `Type mismatch: cannot convert from boolean to WebDriver` and on the until the error is `The method until(Function super WebDriver,V>) in the type FluentWait is not applicable for the arguments (WebDriver)` – nuzo Sep 25 '19 at 21:11
  • This may help https://softwaretestingboard.com/q2a/1907/function-webdriver-fluentwait-webdriver-applicable-arguments#axzz60ZcNcLmZ – CEH Sep 25 '19 at 21:19
  • If you continue having issues with this error message, you might have better luck creating a separate question, since the error message has deviated from the original problem you were trying to solve. – CEH Sep 25 '19 at 21:19
  • my Guava library is version 25 (only one) and my JDK is version 8, i'm working with Maven. im not sure from where it is cause for example `JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollTo(0, document.body.scrollHeight)") ` these is working – nuzo Sep 25 '19 at 21:22
  • The issue is not with the `JavascriptExecutor` anymore, it is the `wait.until` keyword. a few solutions here: https://stackoverflow.com/questions/42421148/wait-untilexpectedconditions-doesnt-work-any-more-in-selenium – CEH Sep 25 '19 at 21:29
  • so i added the new dependency and actualy got the new wait `public V until(Function super T, V> isTrue) {}` even put it as the first dependency but still the same, my question now is how the scrolling with the JS is working like no problem? – nuzo Sep 25 '19 at 21:41
  • these also looks working `void waitForLoad(WebDriver driver) { new WebDriverWait(driver, 30).until((ExpectedCondition) wd -> ((JavascriptExecutor) wd).executeScript("return document.readyState").equals("complete")); }` from here [link](https://stackoverflow.com/a/15124562/12115696) – nuzo Sep 25 '19 at 21:52
  • that looks correct, it's functionally the same as what I posted so if it compiles then you should use that. – CEH Sep 25 '19 at 21:55
  • still your solution will help, so a V on it's was – nuzo Sep 25 '19 at 22:01