• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / What is Explicit wait in Selenium Webdriver

What is Explicit wait in Selenium Webdriver

June 17, 2020 by Mukesh Otwani 38 Comments

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 🙂

Filed Under: Advance Selenium, Dynamic Wait in Selenium Tagged With: Explicit wait in selenium webdriver

Reader Interactions

Comments

  1. Garima says

    July 29, 2021 at 4:43 PM

    Hi Mukesh,

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

    Reply
    • Mukesh Otwani says

      August 3, 2021 at 6:59 AM

      Hi Garima,

      Please follow this link https://www.selenium.dev/documentation/en/webdriver/waits/

      Reply
  2. Dheeraj says

    June 17, 2020 at 4:18 AM

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

    Reply
    • Mukesh Otwani says

      June 18, 2020 at 10:58 AM

      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 🙂
      http://learn-automation.com/implicit-wait-in-selenium-webdriver/

      Reply
  3. vrushali says

    August 23, 2019 at 12:05 PM

    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 ?

    Reply
    • Mukesh Otwani says

      August 23, 2019 at 3:27 PM

      Hi Vrushali,

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

      Reply
      • Seema says

        December 12, 2019 at 5:22 PM

        Hi Mukesh,

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

        Thanks!

        Reply
        • Mukesh Otwani says

          December 12, 2019 at 5:34 PM

          Hi Seema,

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

          Reply
    • Pankaj Vadade says

      July 7, 2021 at 11:53 AM

      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.

      Reply
      • Mukesh Otwani says

        July 7, 2021 at 1:04 PM

        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.

        Reply
  4. Ajmal says

    August 14, 2019 at 10:44 PM

    excellent explanation

    Reply
    • Mukesh Otwani says

      August 14, 2019 at 11:44 PM

      Glad….You’re always welcome…:)

      Reply
  5. Jithin says

    January 17, 2017 at 4:50 PM

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

    Reply
    • Mukesh Otwani says

      January 24, 2017 at 5:03 PM

      Hi Jithin,

      We use wait in our helper classes.

      Reply
  6. Nethra says

    January 16, 2017 at 7:17 AM

    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?

    Reply
    • Mukesh Otwani says

      January 24, 2017 at 4:58 PM

      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.

      Reply
  7. Manjeet says

    December 15, 2016 at 3:09 AM

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

    Reply
    • Mukesh Otwani says

      December 15, 2016 at 7:06 PM

      Hi Manjeet,

      It will throw noSuchElementException

      Reply
  8. sowji says

    September 23, 2016 at 8:10 AM

    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.

    Reply
    • Mukesh Otwani says

      September 23, 2016 at 9:04 AM

      Hi Sowji,

      This is what exactly you will get in below article http://learn-automation.com/fluentwait-in-selenium-webdriver/

      Reply
  9. Vignesh says

    September 20, 2016 at 5:51 PM

    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

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 2:48 PM

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

      Reply
  10. Sachin Patil says

    September 20, 2016 at 9:36 AM

    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.

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 2:23 PM

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

      Reply
  11. sowjanya says

    September 19, 2016 at 11:01 PM

    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

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 2:49 PM

      Hi Sowjanya,

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

      Reply
  12. Soumen says

    September 18, 2016 at 8:22 PM

    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

    Reply
    • Mukesh Otwani says

      September 19, 2016 at 12:21 AM

      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.

      http://learn-automation.com/chrome-browser-on-mac-using-selenium/

      Reply
      • Soumen says

        September 22, 2016 at 9:40 PM

        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

        Reply
        • Mukesh Otwani says

          September 23, 2016 at 2:38 AM

          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.

          Reply
  13. Vipin says

    September 16, 2016 at 9:54 PM

    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

    Reply
    • Mukesh Otwani says

      September 19, 2016 at 1:03 PM

      Hi Vipin,

      Follow below steps for MAC

      http://learn-automation.com/chrome-browser-on-mac-using-selenium/

      Reply
  14. Amod Mahajan says

    September 13, 2016 at 4:04 PM

    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?

    Reply
    • Mukesh Otwani says

      September 15, 2016 at 6:04 PM

      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.

      Reply
      • Guru says

        December 14, 2016 at 1:10 AM

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

        Reply
        • Mukesh Otwani says

          December 17, 2016 at 12:46 PM

          Yes Guru for that you have to use FLuentWait http://learn-automation.com/fluentwait-in-selenium-webdriver/

          Reply
  15. Shreyansh Jain says

    September 12, 2016 at 9:28 AM

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

    Reply
    • Mukesh Otwani says

      September 12, 2016 at 2:50 PM

      Yes I am going to post video tomorrow. You can check below link for more details http://learn-automation.com/fluentwait-in-selenium-webdriver/

      Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • Syllabus For Playwright Online Training Program
  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?