WebDriver and C# - NoSuchElement exception

后端 未结 3 1936
天命终不由人
天命终不由人 2021-01-13 06:29

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

相关标签:
3条回答
  • 2021-01-13 06:41

    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);
    }
    
    0 讨论(0)
  • 2021-01-13 06:44

    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

    0 讨论(0)
  • 2021-01-13 06:49

    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;
        }
    
    0 讨论(0)
提交回复
热议问题