Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can check whether a page is loaded completely or not in web driver?

I am writing some Java Webdriver code to automate my application. How can I correctly check whether the page has been loaded or not? The application has some Ajax calls, too.

I have declared an implicit wait for WebDriver.

like image 292
Raghubansh Avatar asked Jun 12 '12 16:06

Raghubansh


People also ask

How do you check if a page is loaded into Selenium web Driver?

We can get Selenium to recognize that a page is loaded. We can set the implicit wait for this purpose. It shall make the driver to wait for a specific amount of time for an element to be available after page loaded.

How do I know if a website is fully loaded?

After you have a basic understanding of javascript, you can detect when a page has loaded by using the window. onload event. window. onload = function() { addPageContents(); //example function call. }

Which method will wait till page gets loaded fully?

We can wait until the page is completely loaded in Selenium webdriver by using the JavaScript Executor. Selenium can run JavaScript commands with the help of the executeScript method.


2 Answers

Selenium does it for you. Or at least it tries its best. Sometimes it falls short, and you must help it a little bit. The usual solution is Implicit Wait which solves most of the problems.

If you really know what you're doing, and why you're doing it, you could try to write a generic method which would check whether the page is completely loaded. However, it can't be done for every web and for every situation.


Related question: Selenium WebDriver : Wait for complex page with JavaScript(JS) to load, see my answer there.

Shorter version: You'll never be sure.

The "normal" load is easy - document.readyState. This one is implemented by Selenium, of course. The problematic thing are asynchronous requests, AJAX, because you can never tell whether it's done for good or not. Most of today's webpages have scripts that run forever and poll the server all the time.

The various things you could do are under the link above. Or, like 95% of other people, use Implicit Wait implicity and Explicit Wait + ExpectedConditions where needed.

E.g. after a click, some element on the page should become visible and you need to wait for it:

WebDriverWait wait = new WebDriverWait(driver, 10);  // you can reuse this one  WebElement elem = driver.findElement(By.id("myInvisibleElement")); elem.click(); wait.until(ExpectedConditions.visibilityOf(elem)); 
like image 136
Petr Janeček Avatar answered Sep 28 '22 04:09

Petr Janeček


Simple ready2use snippet, working perfectly for me

static void waitForPageLoad(WebDriver wdriver) {     WebDriverWait wait = new WebDriverWait(wdriver, 60);      Predicate<WebDriver> pageLoaded = new Predicate<WebDriver>() {          @Override         public boolean apply(WebDriver input) {             return ((JavascriptExecutor) input).executeScript("return document.readyState").equals("complete");         }      };     wait.until(pageLoaded); } 
like image 21
Narendra Chandratre Avatar answered Sep 28 '22 02:09

Narendra Chandratre