Search code examples
c#seleniumselenium-webdriverwebdriverautomated-tests

Selenium C# WebDriver: Wait until element is present


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...


Solution

  • 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.