We Offer 100% Job Guarantee Courses (Any Degree / Diploma Candidates / Year GAP / Non-IT / Any Passed Outs). Placement Records
Hire Talent (HR):+91-9707 240 250

General

Waits in Selenium WebDriver

Waits in Selenium WebDriver

Selenium WebDriver will always execute according to the way we organize/create our scripts. Let’s start to discuss the way our scripts works  We can take a general example login page when we launch a URL. URL will be launched and the page should be opened. Once if we launched there can be the scenario, where selenium tries to perform the required action even before the objects loads. This would be a case where we might get an exception. In order to avoid such an exception, we should ask our web driver to wait for the element to appear and perform the required action, this is where wait command comes in to picture.

Below are the types of wait available in Selenium WebDriver,

  • Implicit Wait
  • Explicit Wait
  • Fluent Wait

Implicit Wait

We can ask our web driver itself to wait for the specified amount of time using implicit wait

To implement Implicit wait:

Driver.manage().timesout.implicitlywait(TimeOut,TimeUnit.Seconds);

The above code will make our web driver wait for the required amount of seconds. So that required object will be loaded and then we can perform our action.

Explicit Wait

There can be a situation where you might need to wait until any Special Condition.

Any Special Condition here, am saying for e.g.:

  • To wait till any element is visible.
  • To wait till any element is clickable.
  • To wait until any frame to be available.
  • To wait until any element to become invisible. Etc.

In such cases, we can go for an explicit wait.

What is Explicit wait?

Previous we saw about implicit wait which would make our web driver wait for a certain time. When you analyze this in-depth. Here the operation is on Driver instance. What if action is based on the outcome of one Object alone and not on the basis of the entire page,  I would go for Explicit wait.

To implement Explicit wait we need to create the instance of the WebDriverWait Class.

Explicit Waits

Once we have the object created we can use it like below,

objWaitdriver.until(ExpectedConditions.presenceOfElementLocated(driver.findElement(by.id("dsf"));

So once we right the code, now our WebDriver would wait for a specified amount of time(Say here it would be 1000 ms) until the condition gets satisfied.

This is how we can implement an Explicit wait.

Fluent Wait

One more last wait available in Selenium, let’s discuss it, There can be a scenario where some elements given inside a frame would be changing dynamically, though my page does not get refreshed. For example updation of Cricket scores in a webpage or updation of Timings on a webpage.

So when you try to validate the presence of element which appears after every X minutes or There can be a situation where you need to apply to wait for a certain amount of time at a specified number of times(frequency) on an object in those cases we would go for the fluent wait.

Wait<webdriver>objwait=new FluentWait<WebDriver>(objdriver)
.withTimeout(10,TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);

In the above example, we have declared fluent wait with timeout of 10 seconds and the frequency is set to 5 seconds by ignoring “NoSuchElementException” Which means, for the maximum of ten seconds, tool will look for the element presence for every five seconds, else it will throw “NoSuchElementException” These are the multiple waits that can be used in our Projects, Many usually would go for Thread.sleep(), which is not recommended to use, as it would result in high memory utilization and less efficiency.

Related Blogs

 

Besant Technologies WhatsApp