Hello Folks,

We have covered below types of waits present in Selenium WebDriver in older posts:

  1. Thread.sleep()
  2. Implicit Wait.

In this post, we will learn Explicit Wait.

Implicit wait is wait which is set for a session of WebDriver and it will just make WebDriver to wait till it finds the web element. But there are scenarios in which we need to wait even after locating web element. Consider below scenarios:

  1. When user clicks on Login button, a text box appears after some time. In beginning text box is disable but gets enable automatically after some time. If we use implicit wait, it will resume execution as soon as element is found and will fail because element is disable  and it could not perform desire operation on found element. In this scenario, WebDriver needs to wait till element is enable. How we will make webdriver to wait till element is enable and perform some action?
  2. Suppose an alert is open and it takes time to disappear once user accepts or dismisses it.  You need to wait till alert is disappear. How WebDriver will know when alert is disappeared? Thread.sleep will not solve problem always as you never know how much time alert will take to disappear. Implicit wait will also not work.
  3. I want to give one real time example. Many applications use toaster messages to show information. There is time defined for a toaster message to disappear. It appears generally at right top corner. Suppose, at right top corner, there is logout button and when toaster message appears it hides logout button. You want to perform some operations so that a toaster message will appear and you want to click on logout. WebDriver will not able to click on logout immediately as toaster message will hide logout button. Here, you need to make WebDriver to wait till toaster messages disappear. It will give exception stating “Other element will receive the click.” because webdriver will be able to locate logout button but will not be able to click.

So, we can understand sleep or implicit wait will just help if web element is not located immediately. It will not allow flexibility to wait till some specific reason is satisfied. So, to handle these condition based wait, selenium webdriver provides “EXPLICIT WAIT”.

What is Explicit Wait?

An explicit wait is code you define to wait for a certain condition to occur before proceeding further in the code.

There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

Java Code to use Explicit wait:
WebDriver driver= new ChromeDriver();
WebDriverWait wait= new WebDriverWait(driver, 30);
WebElement Element= wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("Some Expression"))));
Element.click();

Explanation:

  • WebDriverWait is a class. We can create an object of it by passing WebDriver instance and timeout in seconds. WebDriverWait will ignore instances of NotFoundException that are encountered (thrown) by default in the ‘until’ condition, and immediately propagate all others. You can add more to the ignore list by calling ignoring(exceptions to add).
  • An exception will be called based on expected condition in case of time out and condition is not satisfied. We can pass timeout only in seconds.
  • ExpectedConditions is a class which has canned ExpectedConditions which are generally useful within webdriver tests. For example: elementToBeClickable etc.
  • All canned expected conditions are static methods of ExpectedConditions class. To view all canned conditions, do”ExpectectedCondition.(dot)”.
  • This waits up to 30 seconds before throwing a TimeoutException or if it finds the element will return it in 0 – 30 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return value for the ExpectedCondition function type is a Boolean value of true, or a non-null object.
  • Like implicit wait, explicit wait will also not wait further if condition is satisfied before timeout reaches.
  • Explicit wait is intelligent wait as it waits dynamically for specified condition.

More about Explicit wait:

  1. Unlike implicit wait, explicit wait is not for entire webdriver session.
  2. Unlike implicit wait, you need to write explicit wait every time whenever you need it as it is specific to a web element.
  3. No need to create object of WebDriverWait again and again if you want similar timeout . If you need another timeout duration or driver instance, go for new instance of WebDriverWait.
  4. We can specify timeout only in seconds. Other units of time are not possible.
  5. You can reduce test execution time by using explicit wait.
  6. We can customized polling time or ignore other exceptions as well using WebDriverWait instance. For example:
    WebDriverWait wait= new WebDriverWait(driver, 30);
    wait.pollingEvery(1, TimeUnit.MICROSECONDS);
    wait.ignoring(ElementNotVisibleException.class);

It is more advisable to use explicit wait as it provides more flexibility in customized wait.

That’s it about explicit wait. Hope you must have good idea after reading this.

If you like my posts, please like, comment , share and subscribe.

Do not forget to check frequently asked java programs in interview.

#HappyLearning

 

11 thoughts on “Part 3: Waits In Selenium : Explicit Wait

  1. Hi Amod,”

    what actualy this line do “wait.ignoring(ElementNotVisibleException.class)” ?? I’m confused? please elaborate more if possible? And also I had one interview question where they asked how do you handle exception without try catch? Is above wait.ignore statement can be used to handle exception ??

    1. Suppose there is a box full of different types of fruits. You asked one of you friend to pick any fruit from box but ignore and choose again if you get Orange as you don’t like it. While performing any action you might get some exceptions and you want to just ignore some specific exceptions. In that case use ignore.

      1. Thanks Amod. Exception will be ignored till time out? and again after time oue that exception will be shown? Im trying to understand.

        1. Yes till the timeout only it will be ignored. After time out, no restrictions on type of exceptions.

        1. Caller method needs to handle that exception and goes in to hierarchy if not handled. You can refer throws concept in java online.

  2. Thanks for the another great post.

    One of the doubt is if we can use pollingEvery and ignoring in this explicit wait. Then whats the use of fluent wait . I was thinking till today that this pollingEvery and ignoring are the features of fluent wait.

Leave a Reply