Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I run nearly 30-40 test cases twice a day. And everytime some test case fails due to page load time. Infact i have explicitly mentioned Thread.sleep("2000"); wherever i feel it takes more time to load. But still some 5-6 testCases fails. How can i make my selenium to wait until the page loads ?? And what would the below code do ? driver.manage().timeouts().implicitlyWait(4000, TimeUnit.SECONDS); Will it wait for 4 seconds everytime or it sets the maximum time limit to 4 secs . I even tried this but it really slowed my test execution. I need some help.

share|improve this question

Look at the Wait commands rather than sleep - a search on this site or Google will give you lots of examples

share|improve this answer
The single most helpful method for avoiding problems like the OP has is to use a "waitForPageToLoad" method. Totally worth it. – joshin4colours Oct 10 '13 at 11:35

You can use WebDriverWait to synch with page load. Something like the following might help

WebDriverWait wait = new WebDriverWait(driver, 15); //you can play with the time integer  to wait for longer than 15 seconds.`
wait.until(ExpectedConditions.titleContains("title")); //if you want to wait for a particular title to show up


wait.until(ExpectedConditions.presenseOfElementLocated(ByLocator)); //in case you want to wait for a particular element to appear on the page.`
share|improve this answer

A Sleep method is almost always the least acceptable way to synchronize an automated test. A better approach might be to register an event and check the event is raised when in the desired state, or use a polling loop which will can enable the test to proceed when the system is ready (e.g. an element id appears), or a timeout occurs if it takes more than a pre-determined max wait time. (See Sleepy Automated Tests)

share|improve this answer
This link doesn't have any details. Says it is for sale. – ilm Feb 6 at 9:11

Don't ever use sleep in your test. Every sleep you have indicates to a bad test design. Let's say you have a sleep of 1000ms, but it takes 1002ms to load something - your test will break. If it takes 996ms, you are wasting time. That been said, I just answered pretty much the same question in stackoverflow -

share|improve this answer
I think you have a point . But for page load can we use WaitForPageLoad() ? . Some times due to slow network my application becomes slow or page load time gets slower . How to handle selenium in those situations ?? – kittudk Sep 26 '13 at 5:55
There is no such thing has "page has finised loading" unless you are serving static html. nowadays stuff getting loaded async all the time and this conception does not exist anymore. you as a tester must know when some specific stuff happens and wait for that very thing to happen before you continue. So you rather wait for (for example) a submit button to be visible and assume the page in a state so you can continue. If you run your tests thru a proxy, the browsermob has something like WaitForNetworkTrafficToStop, but i would go with WebDriverWait. – Erki M. Sep 27 '13 at 7:50

This code tells Selenium to search up to 60 seconds for id-of-element to appear on the page. Once it finds the element, you can interact with it. If the element is not found within 60 seconds, a java.lang.AssertionError will be thrown with the reason being "timeout".

for (int second = 0;; second++){
        if (second >= 60)
            if (1 == driver.findElements(By.xpath("//a[@id=\"id-of-element"]")).size()){
        } catch (Exception e){

As per Kate's comment, this code will not produce a wait time of 60 seconds.

To wait for an element to appear, I would use an implicit wait:

driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

This means that whenever you call WaitForElement() or WaitForElements(), the driver will wait up to 60 seconds for the specified element or elements to appear. See and

share|improve this answer
I don't think this is going to give you a 60 second fuse - all it's going to give is 60 iterations which take whatever time they take - to do something like this you really need to include an explicit wait call for 1 second. – Kate Paulk Sep 17 '13 at 18:33
This is the best answer IMO, except you should wait for a certain date (i.e not + 60 seconds) rather than 60 counts as mentioned, and you could refactor this to a method to take the By selector and any root element or default to the driver to be able to use it as an extension method on IWebDriver or IWebElement so your driver always tries waiting for any element it accessed to be visible. – chrisc Jun 18 '15 at 16:41

driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); is the correct and cleanest way of doing this in my experience. Call it once after instantiating the driver, and all subsequent calls will correctly wait for whatever timespan specified (60 seconds in this case)

It is better to be generous in timeframes for explicit waits, as lower environments can have skewed loading times, especially when it is the first couple calls to a fresh environment. That being said, 60 seconds is still on the high end for web apps.

share|improve this answer
Why not use the explicit waits and wait for a custom condition? – log_file Feb 6 at 19:41

Simple ready2use snippet > Working perfectly for me

static void waitForPageLoad(WebDriver wdriver) {
    WebDriverWait wait = new WebDriverWait(wdriver, 60);

    Predicate<WebDriver> pageLoaded = new Predicate<WebDriver>() {

        public boolean apply(WebDriver input) {
            return ((JavascriptExecutor) input).executeScript("return document.readyState").equals("complete");

share|improve this answer
This thread seems active since long. @kituudk - Resolved your query of page load time? – NarendraC Jul 6 at 6:45
synchronized(driver) {
share|improve this answer
Welcome to SQA, Raman. Could you elaborate on this? As a rule of thumb, a snippet of code is less helpful than the accompanying explanation. – corsiKa Aug 19 '13 at 14:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.