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.
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.
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 🙂
Garima says
Hi Mukesh,
I need to implement Explicit waits using Javascript. Please suggest ,how can I implement it ?
Mukesh Otwani says
Hi Garima,
Please follow this link https://www.selenium.dev/documentation/en/webdriver/waits/
Dheeraj says
If I use both implicit and explicit wait , which wait will get executed first .
Mukesh Otwani says
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/
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 ?
Mukesh Otwani says
Hi Vrushali,
Implicit wait once initialised, remain for entire life of driver object. So you don’t need to mention it again
Seema says
Hi Mukesh,
How about explicit wait? Is it the same case? Once driver() initiated the time remains for its life?
Thanks!
Mukesh Otwani says
Hi Seema,
Explicit Wait is specific for a particular element not till the life of driver.
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.
Mukesh Otwani says
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.
Ajmal says
excellent explanation
Mukesh Otwani says
Glad….You’re always welcome…:)
Jithin says
Thanks for the tutorial.
Which is the good approach? defining explicit wait in POM class or it in Test Class?
Mukesh Otwani says
Hi Jithin,
We use wait in our helper classes.
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?
Mukesh Otwani says
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.
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.
Mukesh Otwani says
Hi Manjeet,
It will throw noSuchElementException
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.
Mukesh Otwani says
Hi Sowji,
This is what exactly you will get in below article http://learn-automation.com/fluentwait-in-selenium-webdriver/
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
Mukesh Otwani says
Yes Vignesh we can do that but not sure about syntax because I have worked mainly on JAVA
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.
Mukesh Otwani says
Yes Sachin, You can set in environment variable then you can use without driver.
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
Mukesh Otwani says
Hi Sowjanya,
Yes if it is web app then yes it will work.
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
Mukesh Otwani says
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/
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
Mukesh Otwani says
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.
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
Mukesh Otwani says
Hi Vipin,
Follow below steps for MAC
http://learn-automation.com/chrome-browser-on-mac-using-selenium/
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?
Mukesh Otwani says
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.
Guru says
Is it possible to increase/decrease the polling MS??
Mukesh Otwani says
Yes Guru for that you have to use FLuentWait http://learn-automation.com/fluentwait-in-selenium-webdriver/
Shreyansh Jain says
I got implicit and explicit wait. Can you explain fluent wait
Mukesh Otwani says
Yes I am going to post video tomorrow. You can check below link for more details http://learn-automation.com/fluentwait-in-selenium-webdriver/