When I try to check whether or not an element exists on my web page, and if the element does not exist, my IWebDriver launches a WebDriverException , and all methods in my driver stop working, I continue to run the test even doing the treatment to return null or 0 in a catch.
I would like to remember the verification method I'm using is in the Selenium documentation, saying to use FindElements instead of FindElement: findElement should not be used to look for non-present elements, use WebDriver.findElements(By) and assert zero length response instead.
I'm using the latest version of Selenium WebDriver and ChromeDriver for C #.
Error reported in Exception: [OpenQA.Selenium.WebDriverException]
The HTTP request to the remote WebDriver server for URL http://localhost:18144/session/5eeb22629a0d6f35b829113728c3f5a7/elements timed out after 60 seconds.'
Method that I use to check whether element exists or not:
public IWebElement SafeFindElement(By by)
{
try
{
var element = _driver.FindElements(by);
return element.FirstOrDefault();
}
catch (NoSuchElementException)
{
return null;
}
catch (Exception)
{
return null;
}
}