Difference between Implicit and Explicit Wait in Selenium


In this post, we will go through the difference between Implicit Wait and Explicit wait in Selenium WebDriver.In addition, we would go through some code example about both of them.





Implicit Wait

We can use Implicit wait for waiting for a web element. So, we would be able to set for all the web elements, that we use in our test scripts. Furthermore, it is generic to all the web elements of the web application. Also, in Selenium, Implicit wait time is common for all the operations for the web driver. Therefore, webDriver Implicit wait is like a global wait time for the whole application. For example, we can set it as shown below:


FirefoxDriver driver=new FirefoxDriver();
WebElement element =driver.findElement(By.partialLinkText("TESTNG"));
Actions action=new Actions(driver);
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
// browser window close


As shown above 45 seconds specifies the amount of time the driver should wait when searching for an element if it is not immediately present.

Single element

WebDriver polls the page for an element.It polls the web page until the element is found or the timeout expires. When timeout expires it throws a NoSuchElementException to the calling method.

Multiple elements

Webdriver when searching for multiple elements, polls the page until at least one element has been found or the timeout expires.

We should exhibit caution while using implicit wait: Increasing the implicitly wait timeout should be used judiciously as it will have an adverse effect on test run time, especially when used with slower location strategies like XPath.

Explicit Wait

Explicit wait is like conditional wait for any specific web element. Also, we may want to wait overriding the implicit wait time. Furthermore, we can specify ExpectedCondition to apply the condition wait. Explicit Wait is an intelligent kind of wait that provides a better approach than that of Implicit Wait.

Example Code snippet: sample method to wait until the visibility of elements on the web page.


public void waitForVisibilityOfElement(By by, String locator) throws Throwable { WebDriverWait wait = new WebDriverWait(driver, 45); try { wait.until(ExpectedConditions.visibilityOfElementLocated(by)); } catch (Exception e) { e.printStackTrace(); } }


WebDriverWait is a subclass of FluentWait class :

public class WebDriverWait extends FluentWait<WebDriver>

FluentWait Example is discussed here: FluentWait Example