Selenium c#: Wait Until Element is Present Without Waiting the Time Given, Else Time Out Selenium c#: Wait Until Element is Present Without Waiting the Time Given, Else Time Out selenium selenium

Selenium c#: Wait Until Element is Present Without Waiting the Time Given, Else Time Out


This is exactly what explicit wait and expected conditions are for.

Uses example

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By locator));

This will wait up to 5 seconds for the element to be visible. If successful the element will be returned, if not it will throw TimeoutException.

Update

ExpectedConditions has moved and is now located in SeleniumExtras.WaitHelpers

'ExpectedConditions' is obsolete: 'The ExpectedConditions implementation in the .NET bindings is deprecated and will be removed in a future release. This portion of the code has been migrated to the DotNetSeleniumExtras repository on GitHub (https://github.com/DotNetSeleniumTools/DotNetSeleniumExtras)'

To avoid many changes in the existing code import the ExpectedConditions to a variable called ExpectedConditions. The rest of the code remains the same

using ExpectedConditions = SeleniumExtras.WaitHelpers.ExpectedConditions;