WebDriver customer wait in Selenium C#
This is the below custom wait function which makes the driver to wait for some seconds to find the element
The below code
- Waits for 10 seconds by Default
- Can be made custom also
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));
}
else
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
return wait.Until(drv => drv.FindElement(by));
}
}
Usage:
WebDriver.FindElement(By.id(“Element”),WaitTime)
Where the wait time is seconds and the WebDriver will wait for WaitTime before throwing up and element not found error