Wait Statement In Selenium WebDriver

What is wait statement?

Wait statement pause the execution process for particular amount of time. During this time WebDriver will in pause state it doesn’t do any activity for specified time.

Why do we need wait statement?

To synchronize execution speed and application page loading speed we required wait statement. In real time scenario application not much speed enough to run with processor speed. Application will take some time to load entire page. So to match the speed with processor and application we need to add wait statement in between the our script.

Types of wait statements

In selenium WebDriver we have basically 3 types of wait statement they are,

Implicit wait

In implicit wait, WebDriver will wait for certain amount of time before it throws “No Such Element Exception” exception. Default time setting is 0 (Zero). Once we define particular time, WebDriver will wait for that particular time before throwing an exception. Implicit wait is applicable to search all elements.

Example:

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

In above example, WebDriver will wait maximum 15 seconds to throw “No Such Element Exception” exception. That means if element to be searched appears within time frame (15 second), WebDriver will proceed with execution else it throw exception. For example, let say if element appears in 2 seconds WebDriver won’t wait for 15 second, after 2 second it perform the action and go further.

Implicit wait accept two parameters, first one is ‘time’ and second one is time units in terms of, NANOSECONDS, MICROSECONDS, MILISECOND, SECONDS, MINUTES, DAYS, HOURS, etc.

Explicit wait:

Explicit waits are applicable only for targeted element with defined expected condition and time. This wait statement is also called as WebDriverWait. This wait also throw exception if element not found once it reached defined wait time.  

Example:

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='submit']")));

In above example, WebDriver will wait maximum 30 seconds to throw “No Such Element Exception” exception. If element is clickable in 5 seconds, after 5 seconds WebDriver perform respective operation and it proceed next it won’t wait for 30 seconds.

Fluent wait:

The fluent wait is used to tell the WebDriver to wait for a particular condition, as well as the frequency with which we want to check the condition before throwing an exception.

Example:

Wait wait = new FluentWait(driver)
.withTimeout(30, SECONDS)
.pollingEvery(3, SECONDS)
.ignoring(NoSuchElementException.class);

WebElement ele = wait.until(new Function(){
public WebElement apply(WebDriver driver){
return driver.findElement(By.id("value"));
}
});

Thus this means that it will check for the element on the web page at every 6 seconds for the maximum time of 60 seconds. If the element is located within this time frame it will perform the operations else it will throw an” ElementNotVisibleException”.

Recommended article for you,

Hey, Now it is your time! Drop a comment if more details needed or if any update requires. Your comments are more valuable to improve our site to help others.

Leave a Comment

Your e-mail address will not be published. Required fields are marked *