+3 votes
1 view
in Devops and Agile by (25.5k 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 (42.6k 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

by (22.5k points)
This solution worked for me, thanks!
by (32.8k points)
Thanks for the clear explanation!
by (39.1k 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 (10.5k points)
This worked for me.
+2 votes
by (27.7k 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))));

by (25.5k points)
Thank you for the explanation.
+2 votes
by (57.2k 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();

by (42.4k points)
Thanks for the detailed answer.
+1 vote
by (92.9k 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 !


Categories

...