This guide will help you to understand implicit wait in Selenium Webdriver and implementation as well. If you have ever worked on any automation tool then you must be aware of Sync issues in the script.
It is one of the biggest pain for an automation engineer to handle sync issues between elements.
Sometimes an application will not able to load elements due to the below issues.
- Network issue
- Application issues
- Browser stopping JavaScript call.
And so on.
By default, Selenium will not wait for an element once the page load completes. It checks for an element on the page then it performs some operation based on your script but if the element is not present then it throws NoSuchElement Exception.
This is one of the interview questions that what is the default timeout in Selenium is.
Answer- The default timeout is ZERO.
I have listed down some most frequent exception of Selenium. I hope it will help you.
Frequently faced exception is Selenium Webdriver
If you are not sure how to handle exception then the below post will help you.
How to handle exception in Selenium Webdriver
Our main intention to use Implicit wait in selenium is to instruct Webdriver that waits for a specific amount before throwing an exception.
I will highly recommend using implicit wait in your every selenium script or if u have created framework then add the same in BaseClass or Utility
Note- Implicit wait in selenium webdriver will be applicable throughout your script and will works on all elements in the script once your specified implicit wait. It is also known as Global wait
Syntax of Implicit wait in selenium webdriver
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Here in above example, I have used TimeUnit as seconds but you have so many options to use
Seconds, Minutes, Days, Hours, Microsecond, Milliseconds, and so on check the below screenshot for more information.
Once you move forward, you will get to know some more Timeout in Selenium like
Script load timeout in Selenium
Implicit wait in selenium Webdriver (Current reading)
Do not confuse with these exceptions because each of this having different usage.
Complete program with usage of implicit wait in selenium webdriver
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class VerifyTitle
{
@Test
public void verifySeleniumTitle()
{
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.learn-automation.com");
// Specify implicit wait of 30 seconds
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// No login id is present on Webpage so this will fail our script.
driver.findElement(By.id("login")).sendKeys(" Selenium Webdriver");
}
}
Console- The above script will fail because no login id is present on the page but it will wait for max 30 seconds before throwing an exception.
Note- This is the max time so if any element is coming before 30 seconds it will move to next element.
Abhinav says
while iam trying to run my java application in eclipse iam getting an error saying that “could not load main class” . can you please help me out with this.
Mukesh Otwani says
Hi Abhinav,
Are you using @Test annotation from TestNG/JUnint? If not, then you need to use main method to execute your code
Siddharth Barthwal says
what is the polling time for implicit wait. Does it recheck after 10 seconds if 10 seconds is the max time mentioned.
Mukesh Otwani says
Hi Siddharth,
It is 0.5 second
venkatesh says
which one will be considered first, when both Implicit and Explicit waits are in picture.
Some scenarios –
when Implicit time out is 20 sec and explicit Time out is 30 sec
What will happen if the element is found out
1. at 10th sec
2. at 25th sec
3. at 31st sec
4. at 50th sec
Mukesh Otwani says
Hi Venkatesh,
First of all Implicit wait will be applied for all elements and Explicit wait works for specific conditions.
Now let’s talk about condition if element found within 20 sec then it will continue with execution else it will throw noSuchElementException.
Thanks
Mukesh Otwani
pooja says
Hi,
is this wait time applicable for the entire script once it is mentioned.
Like will it wait for the element visibility for each and every element in the script?
Mukesh Otwani says
Hi Pooja,
Implicit wait works throughout the script once. But for element visibility, you have to use Explicit wait.
dineshkumar says
what happens if element came after 30 seconds it will throw exceptions
Mukesh Otwani says
Hi Dinesh,
It will throw an exception
Ekta says
Is Implicit wait applicable only for elements which are identified using findElement() method of webdriver instance?
Mukesh Otwani says
Hi Ekta,
Yes, Implicit Wait tell WebDriver to poll DOM while finding an element for given time.
Debanjan Bhattacharjee says
Brilliant explanation on Implicit Wait. Thanks Mukesh !!!
Mukesh Otwani says
Thanks Debanjan 🙂 Keep visiting.
Raja says
I want to execute the code slowly to observe each step, how can i use wait for it
Mukesh Otwani says
Hi Raja,
Set toggle breakpoints inside your script and debug it. This way, you can execute step by step.
pooja says
Hi Mukesh,
Very nice blog i ove it. i have one question.
Explicity wait will use for all the times like thread.sleep();?
Mukesh Otwani says
Hi Pooja,
As the name of wait i.e. Explicit itself explaining its exclusive and specific usage. And no doubt Explicit waits are better than Thread.sleep().
Gaurav Khurana says
Thanks. It’s helpful and easy to implement and best part was the timeout screenshot that you shared which highlight it weighted for 30 seconds. NewBies generally miss such details. Keep up the good work
Mukesh Otwani says
Thanks Gaurav
sudhr says
in tutorial u said that default time is 250 ms.if i mention 30 s. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); how can this work.what is the total wait…plz explain?
Mukesh Otwani says
Hi,
In above scenario if element not found then max time wait will be 30 seconds and if element comes before 30 seconds it will continue to next steps.
sudhr says
what default timeout for implicit wait..
Mukesh Otwani says
Default timeout is ZERO. Default polling time is 250 millisecond if implicit wait given.
venkatesh says
what is the polling time for explicit wait?
Mukesh Otwani says
Hi Venkatesh,
Default polling for Explicit Wait in 500 milliseconds…