+3 votes
1 view
in DevOps and Agile by (30k points)

I want to make sure that an element is present before the web driver starts doing stuff.

I'm trying to get something like this to work:

WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0,0,5));


I'm mainly struggling with how to setup up the anonymous function.

4 Answers

+5 votes
by (51.2k points)

For this issue, you can go with ImplicitWait here is some info about implicit wait

The implicit wait will tell to the web driver to wait for a certain amount of time before it throws a "No Such Element Exception".

 The default setting is 0. Once we set the time, the web driver will wait for that time before throwing an exception.

Here for your question, you can use

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);

For more details of implicit wait refer https://www.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-and-implicit-waits

Also, you can visit Selenium tutorial page for other details.

To learn in-depth about Selenium, sign up for an industry based Selenium course.

by (19.8k points)
This solution worked for me, thanks!
by (33.2k points)
Thanks for the clear explanation!
by (47.2k points)
It is achieved by using Implicit wait for a pre-defined period of time before until when WebDriver will wait to resume execution
by (19.9k points)
This worked for me.
+2 votes
by (29.8k points)

You can also use


So you will search for an element availability like that

new WebDriverWait(driver, TimeSpan.FromSeconds(timeOut)).Until(ExpectedConditions.ElementExists((By.Id(login))));

by (30k points)
Thank you for the explanation.
+2 votes
by (108k points)

Using the solution provided by yeshwanth.intelli may have an impact on overall testing performance since the implicit wait will be used in all FindElement calls. Many times you'll want the FindElement to fail right away when an element is not present (you're testing for a malformed page, missing elements, etc.). With the implicit wait, these operations would wait for the whole timeout to expire before throwing the exception. The default implicit wait is set to 0 seconds.

I've written a little extension method to IWebDriver that adds a timeout (in seconds) parameter to the FindElement() method. It's quite self-explanatory:

public static class WebDriverExtensions


    public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds)


        if (timeoutInSeconds > 0)


            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));

            return wait.Until(drv => drv.FindElement(by));


        return driver.FindElement(by);



I didn't cache the WebDriverWait object as its creation is very cheap, this extension may be used simultaneously for different WebDriver objects, and I only do optimizations when ultimately needed.

Usage is straight-forward:

var driver = new FirefoxDriver();


var btn = driver.FindElement(By.CssSelector("#login_button"));


var employeeLabel = driver.FindElement(By.CssSelector("#VCC_VSL"), 10);

Assert.AreEqual("Employee", employeeLabel.Text);


by (44.6k points)
Thanks for the detailed answer.
+1 vote
by (107k points)

Actually, you are applying wrong logic, it should be as follows:-

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);

Another way to solve this problem would be to use explicit wait see the code below:-

public static WebDriverWait wait = new WebDriverWait(driver, 60);

Welcome to Intellipaat Community. Get your technical queries answered by top developers !