×
☰ See All Chapters

Implicit Wait in Selenium WebDriver

The implicit wait will configure web driver to wait for assigned amount of time before it throws a "No Such Element Exception".  This time is the maximum time selenium webdriver waits while searching an element. WebDriver will not wait strictly to complete this wait time. If element is ready it WebDriver executes the test case immediately. If element is not ready or not available, it wait for the configured time and still if element is not found it throws a "No Such Element Exception".

Syntax

driver.manage().timeouts().implicitlyWait(timeout, timeunit);

timeout: It is an integer value as timeout. e.g.: 10

timeunit: Time unit in terms of SECONDS, MINUTES, MILISECOND, MICROSECONDS, NANOSECONDS, DAYS, HOURS. We use the constants from java.util.concurrent.TimeUnit

Example

import java.util.concurrent.TimeUnit;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class Example {

 

        public static void main(String[] args) {

 

                // configure chromedriver

                System.setProperty("webdriver.chrome.driver", "F:\\My_Programs\\Selenium\\ChromeDriver\\chromedriver.exe");

 

                WebDriver driver = new ChromeDriver();

               

                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

               

                // Launch website

                driver.get("https://www.registration.tools4testing.com/");

               

                // Click on the Login Button

                driver.findElement(By.id("loginopener")).click();

 

                // Focus on the dialog window by click on dialog window tittle

                driver.findElement(By.xpath("//*[@id=\"ui-id-1\"]")).click();

               

                // Enter user name

                driver.findElement(By.id("loginUsername")).sendKeys("manu.m@tools4testing.com");

               

                // Enter user password

                driver.findElement(By.id("loginPassword")).sendKeys("******");

               

                // Click on the Login Button

                driver.findElement(By.id("loginButton")).click();

               

                //wait some time to get login response

                try {

                        Thread.sleep(7000);

                } catch (InterruptedException ie) {

                }

               

                //close the driver

                driver.quit();

               

        }

}


All Chapters
Author