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 WebDriverWait(firefoxDriver, pageLoadTimeout).until(
webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
but even if page is loading above code does not wait.
I know that I can check for particular element to check if its visible/clickable etc but I am looking for some generic solution
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:
Click here for Reference Link
Let me know if you stuck anywhere by implementing.
It overcomes the use of Thread or Explicit Wait.
As you mentioned if there is any generic funtion to check if page has completely loaded in Selenium the answer is No.
First let us have a look at your code trial which is as follows :
The parameter pageLoadTimeout in the above line of code doesn't really reseambles to actual
pageLoadTimeout()
.Here you can find a detailed discussion of pageLoadTimeout in Selenium not working
Now as your usecase relates to page being completely loaded you can use the
pageLoadStrategy()
set tonormal
[ the supported values being none, eager or normal ] using either through an instance of DesiredCapabilities Class or ChromeOptions Class as follows :Using DesiredCapabilities Class :
Using ChromeOptions Class :
You can find a detailed discussion in
Page load strategy for Chrome driver (Updated till Selenium v3.12.0)
Now setting PageLoadStrategy to
NORMAL
and your code trial both ensures that the Browser Client have (i.e. the Web Browser) have attained'document.readyState'
equal to"complete"
. Once this condition is fulfilled Selenium performs the next line of code.You can find a detailed discussion in
Selenium IE WebDriver only works while debugging
But the Browser Client attaining
'document.readyState'
equal to"complete"
still doesn't guarantees that all the JavaScript and Ajax Calls have completed.To wait for the all the JavaScript and Ajax Calls to complete you can write a function as follows :
You can find a detailed discussion in
Wait for ajax request to complete - selenium webdriver
Now, the above two approaches through PageLoadStrategy and "return jQuery.active == 0" looks to be waiting for indefinite events. So for a definite wait you can induce WebDriverWait inconjunction with
ExpectedConditions
set totitleContains()
method which will ensure that the Page Title (i.e. the Web Page) is visible and assume the all the elements are also visible as follows :Now, at times it is possible though the Page Title will match your Application Title still the desired element you want to interact haven't completed loading. So a more granular approach would be to induce WebDriverWait inconjunction with
ExpectedConditions
set tovisibilityOfElementLocated()
method which will make your program wait for the desired element to be visible as follows :I use selenium too and I had the same problem, to fix that I just wait also for the jQuery to load.
So if you have the same issue try this also
You can wrap both function in a method and check until both page and jQuery is loaded
Try this method