tom
tom

Reputation: 1223

WebDriver and C# - NoSuchElement exception

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);
    }

Upvotes: 5

Views: 11159

Answers (3)

Brantley Blanchard
Brantley Blanchard

Reputation: 1218

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;
    }

Upvotes: 6

Jason Kulatunga
Jason Kulatunga

Reputation: 5894

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);
}

Upvotes: 2

Pavel Janicek
Pavel Janicek

Reputation: 14738

well, I am Java guy, so I will not provide you the code, but rather the algorithm:

  • Yours code (I think) should check, if the element is displayed and if not, wait extra 2,5 secs
  • The reason it fails is, that sometimes it takes more than first 2,5 secs to display the element. In that case, check for if the element is displayed will throw exception

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

Upvotes: 1

Related Questions