Selenium C# WebDriver: Wait until element is present

前端 未结 24 3252
礼貌的吻别
礼貌的吻别 2020-11-22 08:53

I want to make sure that an element is present before the webdriver starts doing stuff.

I\'m trying to get something like this to work:

WebDriverWait w         


        
24条回答
  •  时光说笑
    2020-11-22 09:43

    Used Rn222 and Aknuds1 to use an ISearchContext that returns either a single element, or a list. And a minimum number of elements can be specified:

    public static class SearchContextExtensions
    {
        /// 
        ///     Method that finds an element based on the search parameters within a specified timeout.
        /// 
        /// The context where this is searched. Required for extension methods
        /// The search parameters that are used to identify the element
        /// The time that the tool should wait before throwing an exception
        ///  The first element found that matches the condition specified
        public static IWebElement FindElement(this ISearchContext context, By by, uint timeOutInSeconds)
        {
            if (timeOutInSeconds > 0)
            {
                var wait = new DefaultWait(context);
                wait.Timeout = TimeSpan.FromSeconds(timeOutInSeconds);
                return wait.Until(ctx => ctx.FindElement(by));
            }
            return context.FindElement(by);
        }
        /// 
        ///     Method that finds a list of elements based on the search parameters within a specified timeout.
        /// 
        /// The context where this is searched. Required for extension methods
        /// The search parameters that are used to identify the element
        /// The time that the tool should wait before throwing an exception
        /// A list of all the web elements that match the condition specified
        public static IReadOnlyCollection FindElements(this ISearchContext context, By by, uint timeoutInSeconds)
        {
    
            if (timeoutInSeconds > 0)
            {
                var wait = new DefaultWait(context);
                wait.Timeout = TimeSpan.FromSeconds(timeoutInSeconds);
                return wait.Until>(ctx => ctx.FindElements(by));
            }
            return context.FindElements(by);
        }
        /// 
        ///     Method that finds a list of elements with the minimum amount specified based on the search parameters within a specified timeout.
    ///
    /// The context where this is searched. Required for extension methods /// The search parameters that are used to identify the element /// The time that the tool should wait before throwing an exception /// /// The minimum number of elements that should meet the criteria before returning the list /// If this number is not met, an exception will be thrown and no elements will be returned /// even if some did meet the criteria /// /// A list of all the web elements that match the condition specified public static IReadOnlyCollection FindElements(this ISearchContext context, By by, uint timeoutInSeconds, int minNumberOfElements) { var wait = new DefaultWait(context); if (timeoutInSeconds > 0) { wait.Timeout = TimeSpan.FromSeconds(timeoutInSeconds); } // Wait until the current context found the minimum number of elements. If not found after timeout, an exception is thrown wait.Until(ctx => ctx.FindElements(by).Count >= minNumberOfElements); //If the elements were successfuly found, just return the list return context.FindElements(by); } }

    Example usage:

    var driver = new FirefoxDriver();
    driver.Navigate().GoToUrl("http://localhost");
    var main = driver.FindElement(By.Id("main"));
    // It can be now used to wait when using elements to search
    var btn = main.FindElement(By.Id("button"),10);
    btn.Click();
    //This will wait up to 10 seconds until a button is found
    var button = driver.FindElement(By.TagName("button"),10)
    //This will wait up to 10 seconds until a button is found, and return all the buttons found
    var buttonList = driver.FindElements(By.TagName("button"),10)
    //This will wait for 10 seconds until we find at least 5 buttons
    var buttonsMin= driver.FindElements(By.TagName("button"), 10, 5);
    driver.Close();
    

提交回复
热议问题