Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+3 votes
2 views
in DevOps and Agile by (29.3k 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));

wait.Until(By.Id("login"));

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

4 Answers

+5 votes
by (50.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.

Selenium c# Webdriver: Wait Until Element is Present
Intellipaat-community
by (19.7k points)
This solution worked for me, thanks!
by (33.1k 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.5k points)

You can also use

ExpectedConditions.ElementExists

So you will search for an element availability like that

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

Selenium c# Webdriver: Wait Until Element is Present
Intellipaat-community
by (29.3k points)
Thank you for the explanation.
+2 votes
by (107k 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();

driver.Navigate().GoToUrl("http://localhost/mypage");

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

btn.Click();

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

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

driver.Close();

Selenium c# Webdriver: Wait Until Element is Present
Intellipaat-community
by (44.4k points)
Thanks for the detailed answer.
+1 vote
by (106k 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);

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...