Types of Wait in Selenium Using C# Implicit wait Explicit wait Fluent wait Why "Wait" is required in Selenium

Selenium - Wait


Types of Wait in Selenium Using C#

  • Implicit wait
  • Explicit wait
  • Fluent wait

Why "Wait" is required in Selenium

Most of the web applications are developed using Ajax and JavaScript. When a page is loaded by the browser the elements which we want to interact with may load at different time intervals.

Not only it makes this difficult to identify the element but also if the element is not located it will throw an "ElementNotVisibleException" exception. Using Waits, we can resolve this problem.


Implicit Wait

Selenium Web Driver has obtained the idea of implicit waits from Watir.

The implicit wait will tell to the web driver to wait for certain amount of time before it throws a "No Such Element Exception". The default setting is 0. Once we set the time, web driver will wait for that time before throwing an exception.

In the below example we have declared an implicit wait with the time frame of 10 seconds. It means that if the element is not located on the web page within that time frame, it will throw an exception.

If the element is located in 5 seconds, then it will not wait for another 5 seconds as the total seconds to wait is 10, instead it will go for the execution of next immediate statement.

Once we declare an Implicit wait it will be used for all the steps in the page, if the elements are not located then it will wait for specified period of time before throwing an exception.

Implicit Wait is applied to all the elements in the script.

We should note that implicit waits will be in place for the entire time the browser is open

To declare implicit wait:

IWebDriver Driver = new ChromeDriver();

Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);


Explicit Wait

The Explicit wait is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or the maximum time out period before throwing an "NoSucElementException" exception.

The explicit wait is an intelligent kind of wait, but it can be applied only for specified elements. Explicit wait gives better options than that of an implicit wait as it will wait for dynamically loaded Ajax and JavaScript elements.

Once we declare explicit wait it is mandatory to use "ExpectedCondtions" or we can configure how frequently we want to check the condition using Fluent Wait.

Example : In the below snippet we are making the Driver to wait for 10 seconds before web element "Password" is visible by its "Id= Password", if the element is visible before 10 seconds, it will proceed for the execution of next immediate step, else it will throw an exception after completion of 10 seconds.

IWebDriver Driver = new ChromeDriver();


WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));

wait.Until(ExpectedConditions.ElementIsVisible(By.Id("Password")));


Fluent Wait

The Fluent wait used to tell the web driver to wait for a condition, as well as the frequency with which we want to check the condition before throwing an "ElementNotVisibleException" exception.

For each FluentWait , we can specify:

·       Frequency with which FluentWait has to check the conditions defined.

·       Ignore specific types of exception waiting such as NoSuchElementExceptions while searching for an element on the page.

·       Maximum amount of time to wait for a condition 


Let's consider a scenario where an element is loaded at different intervals of time. The element might load within 5 seconds, 12 seconds or even more then that if we declare an explicit wait of 15 seconds. It will wait till the specified time before throwing an exception. In such scenarios, the fluent wait is the ideal wait to use as this will try to find the element at different frequency until it finds it or the final timer runs out.


DefaultWait<IWebDriver> fluentWait = new DefaultWait<IWebDriver>(Driver);
fluentWait.Timeout = TimeSpan.FromSeconds(10);
fluentWait.PollingInterval = TimeSpan.FromMilliseconds(150);
fluentWait.IgnoreExceptionTypes(typeof(NoSuchElementException));

IWebElement searchResult = fluentWait.Until(x => x.FindElement(By.Id("Id_1")));


The following are the Expected Conditions that can be used in Explicit Wait

AlertIsPresent();

AlertState(bool state);

ElementExists(By locator);

ElementIsVisible(By locator);

ElementSelectionStateToBe(By locator, bool selected);

ElementSelectionStateToBe(IWebElement element, bool selected);

ElementToBeClickable(IWebElement element);

ElementToBeClickable(By locator);

ElementToBeSelected(By locator);

ElementToBeSelected(IWebElement element);

ElementToBeSelected(IWebElement element, bool selected);

FrameToBeAvailableAndSwitchToIt(string frameLocator);

FrameToBeAvailableAndSwitchToIt(By locator);

InvisibilityOfElementLocated(By locator);

InvisibilityOfElementWithText(By locator, string text);

PresenceOfAllElementsLocatedBy(By locator);

StalenessOf(IWebElement element);

TextToBePresentInElement(IWebElement element, string text);

TextToBePresentInElementLocated(By locator, string text);

TextToBePresentInElementValue(By locator, string text);

TextToBePresentInElementValue(IWebElement element, string text);

TitleContains(string title);

TitleIs(string title);

UrlContains(string fraction);

UrlMatches(string regex);

UrlToBe(string url);




0 comments: