I have the following code for selecting an option from given list and it usually works, but sometimes it fails with NoSuchElement exception on the second if. I was under the
You could try one of the answers from this SO question:
public static IWebElement FindElement(this IWebDriver driver, String vList, String vText, int timeoutInSeconds)
{
By selector = By.Id(ConfigurationManager.AppSettings[vList])
if (timeoutInSeconds > 0)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(drv => drv.FindElement(selector));
}
return driver.FindElement(selector);
}
well, I am Java guy, so I will not provide you the code, but rather the algorithm:
So, basically you should do some exception handling in the for loop and catch this exception and do nothing. In Java its done by try
and catch
block. But because I dont know C# you will have to find out how its done in this language
Instead of having to try catch every instance, why not create a helper/extension method to take care of that for you. Here it returns the element or returns null if it doesn't exist. Then you can simply use another extension method for .exists().
IWebElement element = driver.FindElmentSafe(By.Id("the id"));
/// <summary>
/// Same as FindElement only returns null when not found instead of an exception.
/// </summary>
/// <param name="driver">current browser instance</param>
/// <param name="by">The search string for finding element</param>
/// <returns>Returns element or null if not found</returns>
public static IWebElement FindElementSafe(this IWebDriver driver, By by)
{
try
{
return driver.FindElement(by);
}
catch (NoSuchElementException)
{
return null;
}
}
bool exists = element.Exists();
/// <summary>
/// Requires finding element by FindElementSafe(By).
/// Returns T/F depending on if element is defined or null.
/// </summary>
/// <param name="element">Current element</param>
/// <returns>Returns T/F depending on if element is defined or null.</returns>
public static bool Exists(this IWebElement element)
{
if (element == null)
{ return false; }
return true;
}