Advance Selenium, Dynamic Wait in Selenium

What is Explicit wait in Selenium Webdriver

Explicit wait in selenium webdriver

To understand the Explicit wait in Selenium Webdriver you should know the requirement why we use wait statements in programs. I will give you a couple of examples in which you will get the complete idea of why wait is important.

Before we move ahead, I would suggest you read about Implicit Wait in Selenium so that you will understand the clear difference between implicit wait and explicit wait. This is one of the most frequently asked questions in interviews as well.

 

Condition for Explicit wait in selenium webdriver

Condition 1- I have a web page which has some login form and after login, it takes a lot of time to load Account page or Home page. This page is dynamic it means sometimes it takes 10 seconds to load the homepage, sometimes its 15 second and so on. In this situation, the Explicit wait can help us which will wait until specific page/page title is not present it will keep waiting.

 

Condition 2- You are working on travel application and you have filled the web form and clicked on submit button. Now you have to wait until complete data is not loaded or specific data is not loaded. In this case, again we can use Explicit wait in which we can give wait till specific or set of elements are not loaded.

 

Condition 3- There are some elements on a web page which are hidden and it will be displayed only when specific conditions get true, so we have to wait until these elements are not visible. In this case, again explicit wait will help in which we can specify wait till the element or elements are not visible.

Explicit wait in selenium webdriver

 

The good thing which will make you happy is that you don’t have to write code for all these conditions. It is already defined in a separate class and we just have to use them based on our requirement.

ExpectedConditions is a class in Selenium which has some predefined condition which makes our task easy.

explicit-wait-in-selenium

 

What is Explicit wait in selenium webdriver

It is a concept of the dynamic wait which wait dynamically for specific conditions. It can be implemented by WebDriverWait class.

 

Syntax of Explicit wait in selenium webdriver

// Create object of WebDriverWait class

WebDriverWait wait=new WebDriverWait(driver,20);



// Wait till the element is not visible

WebElement element=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("ur xpath here")));

 

Now you must be confused between Implicit wait and Explicit wait right?

Don’t worry its totally different let me make it clear for you with the help of one example.

Implicit wait– It only checks the presence of element on WebPage that’s all if elements are hidden or any other condition then it will not handle and it will fail your script.

It is applicable for all the element after initialization.

Explicit wait– It has so much capability which we already discussed and it is applicable to the specific element.

We have one more wait which is FluentWait which is more advance is nature. In interviews, you will definitely get this questions very frequently that what is the difference between Implicit wait, Explicit wait and Fluent Wait in Selenium Webdriver.

 

Note- We can use implicit wait and explicit wait in the single script.

 

                                                       Youtube video for Explicit Wait 

 

Program for Explicit waits in selenium webdriver

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ExplicitWaitDemo {

	public static void main(String[] args) {

		// Start browser
		WebDriver driver = new ChromeDriver();

		// Start application
		driver.get("http://seleniumpractise.blogspot.in/2016/08/how-to-use-explicit-wait-in-selenium.html");

		// Click on timer button to start
		driver.findElement(By.xpath("//button[text()='Click me to start timer']")).click();

		// Create object of WebDriverWait class and it will wait max of 20 seconds.
		// By default it will accepts in Seconds
		WebDriverWait wait = new WebDriverWait(driver, 20);

		// Here we will wait until element is not visible, if element is visible then it will return web element
		// or else it will throw exception
		WebElement element = wait
				.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[text()='WebDriver']")));

		// if element found then we will use- In this example I just called isDisplayed method
		boolean status = element.isDisplayed();

		// if else condition
		if (status) {
			System.out.println("===== WebDriver is visible======");
		} else {
			System.out.println("===== WebDriver is not visible======");
		}

	}

}

 

I hope it must be clear for you regarding dynamic wait in Selenium and when to use which wait in Selenium. If you have any thoughts in mind then let me know in the comment section.

Happy Testing 🙂

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

38 thoughts on “What is Explicit wait in Selenium Webdriver

  1. Garima says:

    Hi Mukesh,

    I need to implement Explicit waits using Javascript. Please suggest ,how can I implement it ?

  2. Dheeraj says:

    If I use both implicit and explicit wait , which wait will get executed first .

    1. Hi Dheeraj,

      Implicit wait is for all web elements and explicit wait is for specific condition so there is no relation between them.
      Implicit wait will wait for WebElement but explicit wait will wait for condition which you have mentioned.
      I hope it is clear now 🙂
      https://vistasadprojects.com/mukeshotwani-blogs-v2/implicit-wait-in-selenium-webdriver/

  3. vrushali says:

    Hi Mukesh ,
    I want to understand ,do we use implicit wait only once so that it will accessible to every step in program i.e. action ?
    i mean for every step is it considering that wait or we need to create it everytime.
    And if we need to define it everytime , can we create the reusable utility for implicit wait ?
    could you plz tell this ?

    1. Hi Vrushali,

      Implicit wait once initialised, remain for entire life of driver object. So you don’t need to mention it again

      1. Seema says:

        Hi Mukesh,

        How about explicit wait? Is it the same case? Once driver() initiated the time remains for its life?

        Thanks!

        1. Hi Seema,

          Explicit Wait is specific for a particular element not till the life of driver.

    2. Pankaj Vadade says:

      Hi vrushali,

      We declare implicit wait once in base class or in Main class so it will applicable on each element while execution. So it increases execution time.

      Instead of that use Explicit Wait –> If we knew at which action the server take times to response.

      1. Hi Pankaj,
        Technically implicit wait once initialized, remains there till the end of the automation script. Implicit Wait has a default timeout of 0 sec. This is the time gap that ideally comes between each webdriver action/step on any WebElement. The best-case scenario for you is to keep Implicit Wait at the lowest value of time. As mentioned, definitely Explicit Wait is a good way to handle it.

  4. Ajmal says:

    excellent explanation

    1. Glad….You’re always welcome…:)

  5. Jithin says:

    Thanks for the tutorial.
    Which is the good approach? defining explicit wait in POM class or it in Test Class?

    1. Hi Jithin,

      We use wait in our helper classes.

  6. Nethra says:

    Hi Mukesh,

    I am a beginner ton automation/ selenium. My code works fine with implicit wait on firefox. When I run the same code in chrome, I get No such element exception. I increased the implicit wait time to 100 seconds but still it throws no such element exception as soon as it logs in before waiting for 100secs (implicit wait time). The only way I could get my code work in chrome is by adding explicit wait on each element. Can you please let me know why is implicit wait not working in chrome in my case. Is there any other solution than adding explicit wait to each element to all my scripts?

    1. Hi Nethra,

      If you are working with cross browsers then sometime xpath will change. Try to use CSS which will remain same for all browsers.

  7. Manjeet says:

    Hi Mukesh,
    What will happen if the element is not found in 20 seconds?
    Thank you. I appreciate. You are a great help.

    1. Hi Manjeet,

      It will throw noSuchElementException

  8. sowji says:

    Hi Sir, I really like your tutorials and videos. Detailed explanation of concepts really helped to me.
    I just wandering I am working on one projectThere is a web element on DashBoard that displaying value is changes until it gets its final value.I would like to get that value but when i tried it fetches random value in between , i could not get the final value on dashboard.
    I know i have to give wait condition on that element but I am not sure which condition has to use.
    I tried couple of conditions
    public String getNoVendors()
    { wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//div[@class=’dashboard-stat blue’]/div[2]/div/span”)));
    String vendorno=driver.findElement(By.xpath(“//div[@class=’dashboard-stat blue’]/div[2]/div/span”)).getText();
    return vendorno;
    }
    could you pls help me.

    I have to get final vendor no=15 as there is 15 vendors available.
    but I got 6 or 13 so on .. on different executions

    I have seen there is a wait condition
    textToBePresentInElementLocated(locator, text)

    my testcase in this situation is need to verify that displayed value.
    Could you pls help me .
    thank you.

  9. Vignesh says:

    Hi Mukesh,

    I am working in C# and using Page Object Model concept. Can we implement Explicit wait in Page Object Model concept. If so could you please tell me how to do that?

    Thanks,
    Vignesh.M

    1. Yes Vignesh we can do that but not sure about syntax because I have worked mainly on JAVA

  10. Sachin Patil says:

    Hi Mukesh,

    Can you please tell if we can launch chrome browser without setting property of chrome driver for Windows machine? Is the process same as MAC or something different.

    1. Yes Sachin, You can set in environment variable then you can use without driver.

  11. sowjanya says:

    Hi Mukesh,

    I have a query. Please help me to solve the issue. After login to website I have to listen to vedio for 1 hour and then need to tune to another vedio.so,here can I use explicit wait? Please explain me on this..

    Thanks,
    sowjanya

    1. Hi Sowjanya,

      Yes if it is web app then yes it will work.

  12. Soumen says:

    HI Sir,

    First of all, I am a big fan of yours. I like your way of explaining any topic . Thank you a lot for the knowledge you shared to us.

    However , in this tutorial (explicit Wait ) I am just wandering how your code is working without using System.SetProperty(“webdriver.chrome.driver”, “chromedriver.exe path”). Could you please tell us the secret.

    Thank you
    Soumen

    1. Hi Soumen,

      Thanks for checking updates regularly and I am glad to know that you liked all.

      I made below changes in my MAC system to work with without driver.

      https://vistasadprojects.com/mukeshotwani-blogs-v2/chrome-browser-on-mac-using-selenium/

      1. Soumen says:

        Hi Sir,

        Thank you for replying .

        I just thought of asking that can we do the same in windows machine by putting in path or by or any trick.

        Thank you
        Soumen

        1. Hi Soumen,

          Yes, we can do if you set the path in Env variable then You don’t have to define the path in every script.

  13. Vipin says:

    Hi Mukesh,
    First of all thanks for this video.
    In this video i saw you write a webdriver code for launching chrome browser without setting property of chrome driver.
    Please let me know how can we launch chrome and ie driver without setting property.

    Thanks,
    Vipin

  14. Amod Mahajan says:

    I have below doubts:
    1. If we do not use any types of wait statements in program, what is default time for which webdriver will search for an element?
    2. Is there any timeout for web page loading in selenium?
    3. Can we increase wait time at run time means can we make webdriver to wait for some extra time to find out en element?

    1. Hi Amod,

      please find my comments inline

      1- by default wait is zero and if some wait are given then by default polling is 250 mili second.
      2- Yes page load timeout is present if you want to set page load timeout. If no timeout given then page will wait until full page loaded.
      3- At run time we cant change the time out.

      1. Guru says:

        Is it possible to increase/decrease the polling MS??

  15. Shreyansh Jain says:

    I got implicit and explicit wait. Can you explain fluent wait

    1. Yes I am going to post video tomorrow. You can check below link for more details https://vistasadprojects.com/mukeshotwani-blogs-v2/fluentwait-in-selenium-webdriver/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.