C# Selenium 'ExpectedConditions is obsolete'

How to resolve this with the latest version of Selenium.

Using NuGet, search for DotNetSeleniumExtras.WaitHelpers, and import that namespace into your class. Now you can do this:

var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30));
var element = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.Id("content-section")));

And the warning in the IDE will be gone.


If you don't want to download an extra NuGet package, it is quite easy to declare your own function (or condition), especially using a lambda expression, e.g.

var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 30));
var element = wait.Until(condition =>
{
    try
    {
        var elementToBeDisplayed = driver.FindElement(By.Id("content-section"));
        return elementToBeDisplayed.Displayed;
    }
    catch (StaleElementReferenceException)
    {
        return false;
    }
    catch (NoSuchElementException)
    {
        return false;
    }
});

This is also very versatile, since it is now possible to evaluate any kind of Boolean expression.


It's very simple. Just change Wait.Until(ExpectedConditions.ElementIsVisible(By.Id("content-section")));

to

Wait.Until(c => c.FindElement(By.Id("content-section")));