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 impression that if it does not find the element it just goes back to through the loop again. I believe the explanation is pretty simple... Could anyone enlighten me?
public static void selectFromList(String vList, String vText, IWebDriver driver)
{
for (int sec = 0; ; sec++)
{
System.Threading.Thread.Sleep(2500);
if (sec >= 10) Debug.Fail("timeout : " + vList);
if (driver.FindElement(By.Id(ConfigurationManager.AppSettings[vList])).Displayed) break;
}
new SelectElement(driver.FindElement(By.Id(ConfigurationManager.AppSettings[vList]))).SelectByText(vText);
}
Conclusion: > Selenium WebDriver supports various programming languages like Java, Python, C#, Ruby, Perl, PHP, JavaScript, R, Objective-C and Haskell.
WebDriver is a remote control interface that enables introspection and control of user agents. It provides a platform- and language-neutral wire protocol as a way for out-of-process programs to remotely instruct the behavior of web browsers.
Selenium is one of the most important automation testing tools because it supports multiple programming languages like Java, Python, C#, Ruby, Perl, and PHP, etc.
Selenium is an open-source, web Automation Testing tool that supports multiple browsers and multiple operating systems. It allows testers to use multiple programming languages such as Java, C#, Python, . Net, Ruby, PHP, and Perl for coding automated tests.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With