Implicit and Explicit Waits in Selenium

Why are waits required in selenium?
In web automation waits are required as certain elements get loaded on the page asynchronously, so after triggering an event a page may get loaded successfully but some of its element may still not get loaded. This causes elementNotFound exception while locating the element. In such cases we are left with using Thread.sleep() i.e. a static wait that will halt the test execution for some specified time and than perform the next step. As Thread.sleep() will wait for the specified time no matter if the elements gets visible before that time. So, using Thread.sleep() is never advisable in UI automation.

To avoid this selenium provides different types of waits out of which Implicit and explicit waits are most commonly used.
Implicit Waits
An implicit wait when used is set to the WebDriver instance and is applied to all the web elements. In implicit wait the webdriver polls the DOM to check the availability of the webElement and waits till the maximum time specified before throwing NoSuchElementException.

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

In the above code snippet the value 20 specified in implicit wait method is the maximum time in seconds till which webDriver will wait before throwing NoSuchElementException while locating a webElement.
Explicit Waits –
Unlike implicit waits, the explicit waits are applied to each and every webElement. In explicit wait certain conditions are defined for which the webDriver instance waits before locating webElements or performing actions on them. Some of the most common conditions specified in explicit waits are-
elementToBeClickable, presenceOfElementLocated etc.

WebDriverWait wait = new WebDriverWait(driver, 15);

Here the webDriver instance will wait until the condition specified is met i.e. the presence Of Element located by the ElementLocator with the maximum wait time of 15 seconds after which if the condition is still not met than it will throw exception.
(Or) You can also use this method….

public WebElement findDynamicElement(By by, int timeOut) {
    WebDriverWait wait = new WebDriverWait(driver, timeOut);
    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
    return element;
findDynamicElement(By.xpath("//body") , 30);


FluentWait Command :
Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such asNoSuchElementExceptions when searching for an element on the page.

 Wait wait = new FluentWait(driver)
    .withTimeout(30, SECONDS)
    .pollingEvery(5, SECONDS)

PageLoadTimeout Command:
Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.
 driver.manage().timeouts().pageLoadTimeout(100, SECONDS);

SetScriptTimeout Command :

Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.




Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s