214

I want to make sure that an element is present before the webdriver 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 how to setup up the anonymous function...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AyKarsi
  • 9,435
  • 10
  • 54
  • 92

28 Answers28

298

Using the solution provided by Mike Kwan may have an impact in 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 straightforward:

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();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Loudenvier
  • 8,362
  • 6
  • 45
  • 66
  • 118
    In case someone wonder, `WebDriverWait` are from the `OpenQA.Selenium.Support.UI` namespace and comes in a separate package called `Selenium WebDriver Support Classes` on NuGet – Andy Sep 11 '13 at 01:55
  • 5
    @Ved i could kiss you <3 been looking for it in a different dll :D – Adween Sep 17 '14 at 14:14
  • 1
    @Loudenvier Please make the first line bold so that it is more noticeable. Especially since it is not the accepted answer, although being a better and more precise approach. – Rick Jul 09 '15 at 07:29
  • @Loudenvier In a case where the html element is not rendered into DOM it will throw NoSuchElementException , appreciate if you update the code which handle those scenario's too – sujith s Nov 04 '16 at 13:53
  • @sujiths this is the expected behavior. Selenium will throw an exception if the element is not found. If you want to change this behavior simply add a `try..catch` for `NoSuchElementException` in the `FindElement` extension method and return `null` when this exception is raised. Then you'll have to check for null whenever you try to find an element with this method. Sometimes it is easier to handle exceptions than to introduce null checks everywhere... It depends on how you write your code. – Loudenvier Nov 04 '16 at 14:03
  • 6
    `Selenium WebDriver Support Classes` is now appeared on NuGet as **"Selenium.Support"**, current version is 3.4.0 – Eric F. Jul 14 '17 at 10:49
  • 1
    I still had a lot of errors until I used this line `return wait.Until(ExpectedConditions.ElementToBeClickable(by));` and it works great now. Heads up in case anyone else gets random elements not found still. – prospector Oct 19 '17 at 05:05
188

Alternatively you can use an implicit wait:

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

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike Kwan
  • 24,123
  • 12
  • 63
  • 96
  • 5
    thanks, the new syntax is : driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); – Reda Jun 07 '13 at 15:16
  • 23
    @RedaBalkouch, the syntax Mike used in his Answer is correct. It's C# – Diemo Apr 22 '14 at 09:44
  • 4
    If you choose to use implicit waits, be careful not to use explicit waits. That can cause some unpredictable behavior leading to bad test results. Generally speaking, I would recommend using explicit waits over implicit waits. – mrfreester Dec 22 '16 at 19:26
  • 7
    This method is now deprecated, you should instead use the property ImplicitWait : `Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);` – Samuel Rondeau-Millaire Mar 31 '17 at 15:17
  • 1
    I used the approach provided and found the method was deprecated as pointed out by Samuel. Checking for the existence of an item now waits up to the specified time. – Jim Scott Jun 01 '17 at 00:47
90

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))));

Source

Zain Ali
  • 15,535
  • 14
  • 95
  • 108
  • 1
    Agreed, this is far more useful than a mere timeout (in cases where you're dynamically loading an object). – keithl8041 Apr 02 '14 at 11:44
  • 8
    Whilst this works. It is now marked as deprecated, so should be avoided. – Adam Garner May 04 '18 at 11:03
  • 4
    Here's the new approach (not deprecated): https://stackoverflow.com/a/49867605/331281 – Dejan Jul 18 '18 at 15:00
  • 2
    Note that, at this time, the `DotNetSeleniumExtras.WaitHelpers` (referred to by @Dejan above) "is not maintained, issues will not be fixed, PRs will not be accepted". (source: https://github.com/SeleniumHQ/selenium/issues/4387#issuecomment-370502866). Its publisher is seeking a maintainer to take it over from him. – urig Oct 28 '19 at 13:53
  • 1
    The link is broken (404). – Peter Mortensen Dec 09 '20 at 21:18
31

Here's a variation of Loudenvier's solution that also works for getting multiple elements:

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);
    }

    public static ReadOnlyCollection<IWebElement> FindElements(this IWebDriver driver, By by, int timeoutInSeconds)
    {
        if (timeoutInSeconds > 0)
        {
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
            return wait.Until(drv => (drv.FindElements(by).Count > 0) ? drv.FindElements(by) : null);
        }
        return driver.FindElements(by);
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rn222
  • 2,167
  • 2
  • 20
  • 36
  • 7
    Nice! I've just added this to my own library! That's the beauty of sharing code!!! – Loudenvier Nov 09 '12 at 18:01
  • 2
    I'd suggest one addition to that. You could catch the NoSuchElement solution and return null in that instance. Then you could create an extension method called .exists that returns true unless the IWebElement is null. – Brantley Blanchard Aug 02 '13 at 20:23
21

Inspired by Loudenvier's solution, here's an extension method that works for all ISearchContext objects, not just IWebDriver, which is a specialization of the former. This method also supports waiting until the element is displayed.

static class WebDriverExtensions
{
    /// <summary>
    /// Find an element, waiting until a timeout is reached if necessary.
    /// </summary>
    /// <param name="context">The search context.</param>
    /// <param name="by">Method to find elements.</param>
    /// <param name="timeout">How many seconds to wait.</param>
    /// <param name="displayed">Require the element to be displayed?</param>
    /// <returns>The found element.</returns>
    public static IWebElement FindElement(this ISearchContext context, By by, uint timeout, bool displayed=false)
    {
        var wait = new DefaultWait<ISearchContext>(context);
        wait.Timeout = TimeSpan.FromSeconds(timeout);
        wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
        return wait.Until(ctx => {
            var elem = ctx.FindElement(by);
            if (displayed && !elem.Displayed)
                return null;

            return elem;
        });
    }
}

Example usage:

var driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://localhost");
var main = driver.FindElement(By.Id("main"));
var btn = main.FindElement(By.Id("button"));
btn.Click();
var dialog = main.FindElement(By.Id("dialog"), 5, displayed: true);
Assert.AreEqual("My Dialog", dialog.Text);
driver.Close();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aknuds1
  • 65,625
  • 67
  • 195
  • 317
  • 1
    If you've set an implicit wait such as `_webDriver.Manage().Timeouts().ImplicitlyWait(Timeout);` that will still trump the timeout value you set here. – howcheng Mar 18 '15 at 00:04
  • This doesn't seem to work for me...? I added a `Stopwatch` around the call to the extension method and a `Console.WriteLine()` inside the lambda sent to `Until()`. The stopwatch measured almost exactly 60 seconds and only one message was written to `Console`. Am I missing something here? – urig Oct 28 '19 at 16:08
13

I confused an anonymous function with a predicate. Here's a little helper method:

   WebDriverWait wait;
    private void waitForById(string id)
    {
        if (wait == null)
            wait = new WebDriverWait(driver, new TimeSpan(0, 0, 5));

        //wait.Until(driver);
        wait.Until(d => d.FindElement(By.Id(id)));
    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AyKarsi
  • 9,435
  • 10
  • 54
  • 92
6

You can find out something like this in C#.

This is what I used in JUnit - Selenium

WebDriverWait wait = new WebDriverWait(driver, 100);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("submit")));

Do import related packages.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
WomenInTech
  • 1,141
  • 2
  • 18
  • 25
  • 2
    I attempted to use this today and VS.net is giving me warnings: the OpenQA.Selenium.Support.UI.ExpectedConditions class has been marked "deprecated" and was "migrated to DotNetSeleniumExtras" repo on https://github.com/DotNetSeleniumTools – Jeff Mergler Apr 19 '18 at 22:02
3
public bool doesWebElementExist(string linkexist)
{
     try
     {
        driver.FindElement(By.XPath(linkexist));
        return true;
     }
     catch (NoSuchElementException e)
     {
        return false;
     }
}
vines
  • 5,160
  • 1
  • 27
  • 49
Madhu
  • 479
  • 4
  • 10
3

Try this code:

 New WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(Function(d) d.FindElement(By.Id("controlName")).Displayed)
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
  • 5
    You should explain what you have done and why this solves the problem. And please format your code. – hering Aug 17 '17 at 13:34
3

I'm using this, and it works very well:

public static bool elexists(By by, WebDriver driver)
{
    try
    {
        driver.FindElement(by);
        return true;
    }
    catch (NoSuchElementException)
    {
        return false;
    }
}

public static void waitforelement(WebDriver driver, By by)
{
    for (int i = 0; i < 30; i++)
    {
        System.Threading.Thread.Sleep(1000);
        if (elexists(by, driver))
        {
            break;
        }
    }
}

Of course you can add more than 30 attempts and shorten the period to less than 1 second to check.

Usage:

waitforelement(driver, By.Id("login"));
IWebElement login = driver.FindElement(By.Id("login"));
login.Click();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Atanas Atanasov
  • 203
  • 1
  • 4
3
// Wait up to 5 seconds with no minimum for a UI element to be found
WebDriverWait wait = new WebDriverWait(_pagedriver, TimeSpan.FromSeconds(5));
IWebElement title = wait.Until<IWebElement>((d) =>
{
    return d.FindElement(By.ClassName("MainContentHeader"));
});
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brian121212
  • 306
  • 3
  • 6
2

The clickAndWait command doesn't get converted when you choose the Webdriver format in the Selenium IDE. Here is the workaround. Add the wait line below. Realistically, the problem was the click or event that happened before this one—line 1 in my C# code. But really, just make sure you have a WaitForElement before any action where you're referencing a "By" object.

HTML code:

<a href="http://www.google.com">xxxxx</a>

C#/NUnit code:

driver.FindElement(By.LinkText("z")).Click;
driver.WaitForElement(By.LinkText("xxxxx"));
driver.FindElement(By.LinkText("xxxxx")).Click();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
1

Explicit Wait

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

Example:

wait.until(ExpectedConditions.visibilityOfElementLocated(UiprofileCre.UiaddChangeUserLink));
Pavan T
  • 716
  • 9
  • 12
1

You do not want to wait too long before the element changes. In this code the webdriver waits for up to 2 seconds before it continues.


WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMilliseconds(2000));
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.Name("html-name")));

ale10ander
  • 942
  • 5
  • 22
  • 42
1

Used Rn222's answer and aknuds1's answer to use an ISearchContext that returns either a single element, or a list. And a minimum number of elements can be specified:

public static class SearchContextExtensions
{
    /// <summary>
    ///     Method that finds an element based on the search parameters within a specified timeout.
    /// </summary>
    /// <param name="context">The context where this is searched. Required for extension methods</param>
    /// <param name="by">The search parameters that are used to identify the element</param>
    /// <param name="timeOutInSeconds">The time that the tool should wait before throwing an exception</param>
    /// <returns> The first element found that matches the condition specified</returns>
    public static IWebElement FindElement(this ISearchContext context, By by, uint timeOutInSeconds)
    {
        if (timeOutInSeconds > 0)
        {
            var wait = new DefaultWait<ISearchContext>(context);
            wait.Timeout = TimeSpan.FromSeconds(timeOutInSeconds);
            return wait.Until<IWebElement>(ctx => ctx.FindElement(by));
        }
        return context.FindElement(by);
    }
    /// <summary>
    ///     Method that finds a list of elements based on the search parameters within a specified timeout.
    /// </summary>
    /// <param name="context">The context where this is searched. Required for extension methods</param>
    /// <param name="by">The search parameters that are used to identify the element</param>
    /// <param name="timeoutInSeconds">The time that the tool should wait before throwing an exception</param>
    /// <returns>A list of all the web elements that match the condition specified</returns>
    public static IReadOnlyCollection<IWebElement> FindElements(this ISearchContext context, By by, uint timeoutInSeconds)
    {

        if (timeoutInSeconds > 0)
        {
            var wait = new DefaultWait<ISearchContext>(context);
            wait.Timeout = TimeSpan.FromSeconds(timeoutInSeconds);
            return wait.Until<IReadOnlyCollection<IWebElement>>(ctx => ctx.FindElements(by));
        }
        return context.FindElements(by);
    }
    /// <summary>
    ///     Method that finds a list of elements with the minimum amount specified based on the search parameters within a specified timeout.<br/>
    /// </summary>
    /// <param name="context">The context where this is searched. Required for extension methods</param>
    /// <param name="by">The search parameters that are used to identify the element</param>
    /// <param name="timeoutInSeconds">The time that the tool should wait before throwing an exception</param>
    /// <param name="minNumberOfElements">
    ///     The minimum number of elements that should meet the criteria before returning the list <para/>
    ///     If this number is not met, an exception will be thrown and no elements will be returned
    ///     even if some did meet the criteria
    /// </param>
    /// <returns>A list of all the web elements that match the condition specified</returns>
    public static IReadOnlyCollection<IWebElement> FindElements(this ISearchContext context, By by, uint timeoutInSeconds, int minNumberOfElements)
    {
        var wait = new DefaultWait<ISearchContext>(context);
        if (timeoutInSeconds > 0)
        {
            wait.Timeout = TimeSpan.FromSeconds(timeoutInSeconds);
        }

        // Wait until the current context found the minimum number of elements. If not found after timeout, an exception is thrown
        wait.Until<bool>(ctx => ctx.FindElements(by).Count >= minNumberOfElements);

        // If the elements were successfuly found, just return the list
        return context.FindElements(by);
    }

}

Example usage:

var driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://localhost");
var main = driver.FindElement(By.Id("main"));
// It can be now used to wait when using elements to search
var btn = main.FindElement(By.Id("button"), 10);
btn.Click();
// This will wait up to 10 seconds until a button is found
var button = driver.FindElement(By.TagName("button"), 10)
// This will wait up to 10 seconds until a button is found, and return all the buttons found
var buttonList = driver.FindElements(By.TagName("button"), 10)
// This will wait for 10 seconds until we find at least 5 buttons
var buttonsMin = driver.FindElements(By.TagName("button"), 10, 5);
driver.Close();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
havan
  • 164
  • 2
  • 11
1

Since I'm separating page elements definitions and page test scenarios using an already-found IWebElement for visibility, it could be done like this:

public static void WaitForElementToBecomeVisibleWithinTimeout(IWebDriver driver, IWebElement element, int timeout)
{
    new WebDriverWait(driver, TimeSpan.FromSeconds(timeout)).Until(ElementIsVisible(element));
}

private static Func<IWebDriver, bool> ElementIsVisible(IWebElement element)
{
    return driver => {
        try
        {
            return element.Displayed;
        }
        catch(Exception)
        {
            // If element is null, stale or if it cannot be located
            return false;
        }
    };
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Angel Dinev
  • 399
  • 4
  • 13
1

This is the reusable function to wait for an element present in the DOM using an explicit wait.

public void WaitForElement(IWebElement element, int timeout = 2)
{
    WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromMinutes(timeout));
    wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
    wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException));
    wait.Until<bool>(driver =>
    {
        try
        {
            return element.Displayed;
        }
        catch (Exception)
        {
            return false;
        }
    });
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Balakrishna
  • 53
  • 1
  • 7
1

You can use the following:

Use namespace:

using SeleniumExtras.WaitHelpers;

In code:

WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, 5));
wait.Until(ExpectedConditions.ElementExists(By.Id("login")));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rony Barua
  • 93
  • 1
  • 5
0

I see multiple solutions already posted that work great! However, just in case anyone needs something else, I thought I would post two solutions that I personally used in Selenium C# to test if an element is present!

public static class IsPresent
{
    public static bool isPresent(this IWebDriver driver, By bylocator)
    {

        bool variable = false;
        try
        {
            IWebElement element = driver.FindElement(bylocator);
            variable = element != null;
        }
        catch (NoSuchElementException){

        }
        return variable;
    }
}

Here is the second:

public static class IsPresent2
{
    public static bool isPresent2(this IWebDriver driver, By bylocator)
    {
        bool variable = true;
        try
        {
            IWebElement element = driver.FindElement(bylocator);
        }
        catch (NoSuchElementException)
        {
            variable = false;
        }
        return variable;
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
newITguy
  • 155
  • 1
  • 11
0

Python:

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

driver.find_element_by_id('someId').click()

WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.ID, 'someAnotherId'))

From EC (import of expected_conditions), you can choose other conditions as well. Try this: Expected conditions Support

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

We can achieve that like this:

public static IWebElement WaitForObject(IWebDriver DriverObj, By by, int TimeOut = 30)
{
    try
    {
        WebDriverWait Wait1 = new WebDriverWait(DriverObj, TimeSpan.FromSeconds(TimeOut));
        var WaitS = Wait1.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.PresenceOfAllElementsLocatedBy(by));
        return WaitS[0];
    }
    catch (NoSuchElementException)
    {
        Reports.TestStep("Wait for Element(s) with xPath was failed in current context page.");
        throw;
    }
}
Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
Krunal
  • 61
  • 1
  • 10
0

WebDriverWait won't take effect.

var driver = new FirefoxDriver(
    new FirefoxOptions().PageLoadStrategy = PageLoadStrategy.Eager
);
driver.Navigate().GoToUrl("xxx");
new WebDriverWait(driver, TimeSpan.FromSeconds(60))
    .Until(d => d.FindElement(By.Id("xxx"))); // A tag that close to the end

This would immediately throw an exception once the page was "interactive". I don't know why, but the timeout acts as if it does not exist.

Perhaps SeleniumExtras.WaitHelpers works, but I didn't try. It's official, but it was split out into another NuGet package. You can refer to C# Selenium 'ExpectedConditions is obsolete'.

I use FindElements and check Count == 0. If true, use await Task.Delay. It's really not quite efficient.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
imba-tjd
  • 491
  • 4
  • 11
0

You can use the following

WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0,0,5));
wait.Until(ExpectedConditions.ElementToBeClickable((By.Id("login")));
Thilanka89
  • 31
  • 2
0

Using C# Extension Method : we can solve the problem of wait until element to be visible.
Max reties for a particular element is 100.

public static bool WaitForElementToBeVisible(IWebDriver browser, By by)
        {
            int attemptToFindElement = 0;
            bool elementFound = false;
            IWebElement elementIdentifier = null;
            do
            {
                attemptToFindElement++;
                try
                {
                    elementIdentifier = browser.FindWebElement(by);
                    elementFound = (elementIdentifier.Displayed && elementIdentifier.Enabled) ? true : false;
                }
                catch (Exception)
                {
                    elementFound = false;
                }

            }
            while (elementFound == false && attemptToFindElement < 100);

            return elementFound;
        }
AmitKS
  • 132
  • 4
0

Mostly I use the following approach, which I also implemented in my latest project SeleniumSharper, which aims to simplify and enhance the Selenium experience.

What's cool about the approach is, that it will work with any instance of ISearchContext, which means you can either call the Wait() method with an IWebDriver or IWebElement.

First, you need to implement this extension method.

public static class SearchContextExtensions
{
    public static Waiter<ISearchContext> Wait(this ISearchContext searchContext, int timeoutInSeconds = 0)
    {
        var wait = new DefaultWait<ISearchContext>(searchContext)
        {
            Timeout = TimeSpan.FromSeconds(timeoutInSeconds)
        };

        return new Waiter<ISearchContext>(wait);
    }
}

where Waiter.cs looks like this

public sealed class Waiter<T>
{
    private readonly DefaultWait<T> _wait;

    public Waiter(DefaultWait<T> wait)
    {
        _wait = wait;
    }

    public TResult Until<TResult>(Func<T, TResult> condition)
    {
        return _wait.Until(condition);
    }
}

After that you need to create a specific condition to satisfy, for example:

public static class WaitConditions
{
    public static Func<ISearchContext, IWebElement> ElementExists(By by)
    {
        return (searchContext) => searchContext.FindElement(by);
    }
}

You can then wait for the condition by chaining up all the methods:

var webdriver = new ChromeDriver();

var locator = By.CssSelector(".myCssClass");

webdriver.Wait(30).Until(WaitConditions.ElementExists(locator));
Maik Hasler
  • 1,064
  • 6
  • 36
-1
 new WebDriverWait(driver, TimeSpan.FromSeconds(10)).
   Until(ExpectedConditions.PresenceOfAllElementsLocatedBy((By.Id("toast-container"))));
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
david
  • 1
-1

The first answer is good, but my problem was that unhandled exceptions didn't close web driver properly, and it kept the same first value I had used which was 1 second.

If you get the same problem, restart your Visual Studio and ensure that all the exceptions are handled properly.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pete Kozak
  • 493
  • 1
  • 4
  • 21
-2

Here is how to wait in Selenium for a condition:

    WebDriverWait wait = new WebDriverWait(m_driver, TimeSpan.FromSeconds(10));
    wait.Until(d => ReadCell(row, col) != "");

ReadCell(row, col) != "" can be any condition. Like this way because:

  • it's mine
  • allows inlining
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mars Robertson
  • 12,673
  • 11
  • 68
  • 89