Using Explicit Wait and Fluent Wait in Selenium

Himanshu Sheth

Posted On: March 27, 2020

view count76034 Views

Read time13 Min Read

This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium C# Tutorial.

Wait is an important command used in Selenium for handling dynamic loading of web elements on a web page (or web application). Wait command in Selenium helps to ensure that our web application is less flaky and is more reliable.

In the previous section of thisSelenium C# tutorial, we discussed the basics of Selenium Wait and implicit wait command.

In this Selenium C# tutorial i’m going to walk you through two more types of Selenium waits in the WebDriver – Explicit wait and Fluent waits.

What is Explicit Wait in Selenium C#

Unlike implicit wait, explicit wait in Selenium will wait for certain conditions to occur. The conditions could be waiting for the presence of the web element, waiting for the element to be clickable, waiting for the element to be visible, etc.

Explicit wait in Selenium is also called smart wait as the wait is not for the maximum time-out. If the condition for explicit wait is satisfied, the wait condition is exited and the execution proceeds with the next line of code. Depending on the test scenario, you should choose the best-suited wait condition for explicit wait.

Unlike implicit wait that applies till the time the Selenium WebDriver instance (or IWebDriver object) is alive, explicit wait in Selenium works only on the particular web element on which it is set, rather than all the elements on the page.

Explicit wait in Selenium can be used in test scenarios where synchronization is needed i.e. loading of the web page is complete and you are waiting for the element (under test) to be visible.

The figure shows what happens under the hoods when explicit wait in Selenium is triggered:

Explicit Wait In Selenium

These steps are followed in sequence when Explicit Wait command is executed:

  • The wait time is entered as a part of explicit wait command [e.g. WebDriverWait wait = WebDriverWait(driver, TimeSpan.FromSeconds(10));]
  • Condition mentioned in .until() method is checked
  • If the condition is not satisfied, a thread sleep is called at the frequency mentioned in the .pollingInterval property. By default, the polling interval is 250 ms.
  • Step (c) is repeated till the timeout of the wait time mentioned in step (a) or exit before timeout is performed, if required web element is located. The status of .until() condition is monitored at the end of every polling duration

Features of Explicit Wait in Selenium

Apart from explicit wait, developers also have the choice of using implicit wait command in Selenium C#. Explicit wait in Selenium has a number of key features (or differences) than other types of Selenium waits:

  • Unlike implicit wait, the explicit wait command in Selenium is documented and has a defined behavior.
  • Explicit wait executes on the local part of Selenium i.e. the programming language of your code, whereas implicit wait works on the remote part of Selenium i.e. the one controlling the web browser.
  • It can work with any possible condition (e.g. elementToBeClickable, alertIsPresent, invisibilityOfElementWithText, , stalenessOf, etc.) unlike implicit wait that only works on findelement methods.
  • Explicit wait in Selenium can also be used in case you are checking for the absence of a web element on the page.
  • The delay between retries can be customized using adjusting the .pollingInterval property which is by default set to 250 ms. On the other hand, delay in implicit waits can only be customized through the global timeout.

Setting Up Selenium In Visual Studio

WebDriverWait and ExpectedCondition

Explicit wait in Selenium is facilitated by WebDriverWait and ExpectedCondition classes. WebDriverWait is present in the OpenQA.Selenium.Support.UI namespace in C#. ExpectedCondition provides a set of conditions on which wait can be performed.

When a search process for a particular web element is performed, the Selenium WebDriver polls the browser for the presence of the element in the DOM (Document Object Model). Below are some of the exceptional situations:

  • NoSuchElementException – The element is not present in the DOM when the search operation is performed.
  • StaleElementReferenceException – The web element is present in the DOM when the search is initiated but the element might have become stale (or its state in the DOM could have changed) when the search call is made.
  • ElementNotVisibleException – The web element is present in the DOM but it is not yet visible when the search process is initiated.
  • ElementNotSelectableException – The element is present on the page but it cannot be selected.
  • NoSuchFrameException – The WebDriver tries switching to a frame which is not a valid one.
  • NoAlertPresentException – The WebDriver attempts switching to an alert window which is not yet available.
  • NoSuchWindowException – The WebDriver attempts switching to a window that is not a valid one.

For avoiding these exceptions, the description of the exception should be passed to the IgnoreExceptionTypes() method

ExpectedConditions class in Selenium C# supplies a set of conditions that can be waited for using WebDriverWait. ExpectedConditions is present in the OpenQA.Selenium.Support.UI.ExpectedConditions namespace.

Some of the common methods exposed by the ExpectedConditions class:

  • AlertIsPresent()
  • ElementIsVisible()
  • ElementExists()
  • ElementToBeClickable(By)
  • ElementToBeClickable(IWebElement)
  • ElementToBeSelected(By)
  • ElementToBeSelected(IWebElement)
  • ElementToBeSelected(IWebElement, Boolean)
  • TitleContains()
  • UrlContains()
  • UrlMatches()
  • VisibilityOfAllElementsLocatedBy(By)
  • VisibilityOfAllElementsLocatedBy(ReadOnlyCollection)
  • StalenessOf(IWebElement)
  • TextToBePresentInElement()
  • TextToBePresentInElementValue(IWebElement, String)

More details about the ExpectedConditions class can be found here.

Take this certification to master the fundamentals of Selenium automation testing with C# and prove your credibility as a tester.

Here’s a short glimpse of the Selenium C# 101 certification from LambdaTest:

Demonstration of Explicit Wait in Selenium C#

To demonstrate the usage of explicit wait in Selenium C#, we perform a search for LambdaTest on Google. For the examples demonstrated in this Selenium C# tutorial, we use the NUnit test framework.

The OpenQA.Selenium.Support.UI & SeleniumExtras.WaitHelpers namespaces are included to use WebDriverWait and ExpectedConditions respectively.

An explicit wait in Selenium with a timeout of 10 seconds is set using the WebDriverWait class.

The ExpectedCondition used is ElementExists. An explicit wait in Selenium is performed till the time the required web element is not found (via XPath) or a timeout occurs i.e. the web element does not exist on the DOM.

As shown in the VS-2019 screenshot, the target web page (LambdaTest Home Page) opens up after search and the test result is Success.

C#result for Explicit Wait in Selenium

Now that we’ve covered what is explicit wait in Selenium test Automation and what are its features in this Selenium C# tutorial. We’ll now move onto the fluent wait in Selenium and discuss it in further detail.

What is Fluent Wait in Selenium C#

Fluent Wait is another Wait variant in Selenium C# that lets you control the maximum amount of time the Selenium WebDriver needs to wait for the defined condition to appear. Fluent Wait functionality in Selenium C# can be achieved by defining the frequency at which the WebDriver checks for the element before it throws ElementNotVisibleException.

One major difference between fluent wait and explicit wait in Selenium test automation is that the polling frequency (.pollingInterval) at which the presence for the web element is checked is controllable in fluent wait in Selenium, whereas it is 250 ms in explicit wait.

If the polling frequency in fluent wait is not set, it defaults to 250 ms. The user also has the flexibility to ignore exceptions that may occur during the polling period using the IgnoreExceptionTypes command. The DefaultWait class in C# is used to configure the timeout and polling interval on the fly.

Syntax of Fluent Wait command in Selenium C#

Demonstration of Fluent Wait in Selenium C#

To demonstrate fluent wait in Selenium test automation, the same test scenario which we used for explicit wait in Selenium, i.e. searching for LambdaTest on Google and clicking on the matching result.

The polling frequency is set to 250 ms and timeout for locating the web element is set to 5 seconds. NoSuchElementException is ignored in case if the web element is not found on the DOM.

The .until() method is used to search for the intended web element. The element is searched till the timeout (of 5 seconds) happens or until the element is found.

As seen in the execution snapshot; the element is found, the target page (LambdaTest homepage) is opened, and the WebDriver session is terminated.

Final result for Explicit wait in Selenium

Wrapping it up

In this Selenium C# tutorial, we had a detailed look at explicit and fluent wait in Selenium. Both explicit and fluent wait in Selenium are ideal for performing waits on web elements in modern day websites as the wait can be used alongside ExpectedConditions.

Explicit and Fluent wait in Selenium look for the presence of the web element at regular intervals till the element is found or there is a timeout occurrence. By using Fluent wait in Selenium test automation, you can control the polling frequency (which is set default to 250 ms) and also configure exceptions that have to be ignored during the polling period.

So far in our Selenium C# tutorials we’ve covered How to set up Selenium in visual Studio,Using Implicit Wait in Selenium. There is a lot more content we’ve planned for this series, also if there’s any topic you want us to cover, do let us know.

I will see you in the next tutorial for this Selenium C# tutorial series, where I’ll show you how to handle alert windows in Selenium C#. That’s all folks!!! Let’s Automate!

Update: We’ve now completed the Selenium C# tutorial series, so in order to help you easily navigate through the tutorials, we’ve compiled the complete list of tutorials, which you can find in the section below.

Selenium C# Tutorials With Examples

Frequently Asked Questions (FAQs)

How to use explicit wait in selenium?

Explicit waits in Selenium are essential for handling dynamic elements. To use them, specify the maximum waiting time and the condition to be met. With the WebDriverWait class, you can apply conditions like element visibility or clickability, enhancing test stability and reliability.

How to give explicit wait for gettitle in selenium?

To give an explicit wait for the getTitle method in Selenium, you can use the WebDriverWait class. Set the desired time limit and condition using the until method, specifying the expected title. Finally, call getTitle within the explicit wait block to wait for the desired title to appear.

How to give explicit wait until going to a particular page in selenium?

To implement an explicit wait until navigating to a specific page in Selenium, use the “WebDriverWait” class and its “until” method. Specify the expected condition, such as the presence of a certain element, and set the desired timeout. Then, apply the wait until the condition is met, ensuring smoother test execution.

How to give explicit wait until an element is clicked in selenium?

To give an explicit wait until an element is clicked in Selenium, you can use the “WebDriverWait” class along with the “ExpectedConditions” method. Use the “elementToBeClickable” condition and specify the maximum time to wait. This ensures the script waits until the element is clickable before proceeding.

What is implicit wait and explicit wait in selenium?

In Selenium, implicit wait and explicit wait are two types of wait mechanisms used for synchronizing test automation scripts with web applications. Implicit wait sets a global wait time for all elements, while explicit wait applies specific wait conditions to targeted elements. Both enhance script stability and reliability.

How to add a 2 second explicit wait in python selenium?

To add a 2-second explicit wait in Python Selenium, use the WebDriverWait class with the time.sleep() method. Set the desired wait time in seconds (here, 2) and use WebDriverWait(driver, time).until() with an expected condition. For example, WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.ID, 'element_id'))).

What is the default polling time for explicit wait in selenium?

The default polling time for explicit wait in Selenium is 500 milliseconds.

What is the explicit wait in selenium?

Explicit wait in Selenium is a powerful feature that allows the automation script to wait for a specific condition to occur before proceeding further. It waits for a defined period until the condition is met, enhancing test stability and synchronization in web testing.

Author Profile Author Profile Author Profile

Author’s Profile

Himanshu Sheth

Himanshu Sheth is a seasoned technologist and blogger with more than 15+ years of diverse working experience. He currently works as the 'Lead Developer Evangelist' and 'Senior Manager [Technical Content Marketing]' at LambdaTest. He is very active with the startup community in Bengaluru (and down South) and loves interacting with passionate founders on his personal blog (which he has been maintaining since last 15+ years).

Blogs: 132


Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free