Selenium C# WebDriver: Wait until element is present

前端 未结 24 2853
礼貌的吻别
礼貌的吻别 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:28

    Here's a variation of @Loudenvier's solution that also works for getting multiple elements:

    public static class WebDriverExtensions
    {
        public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds)
        {
            if (timeoutInSeconds > 0)
            {
                var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
                return wait.Until(drv => drv.FindElement(by));
            }
            return driver.FindElement(by);
        }
    
        public static ReadOnlyCollection<IWebElement> FindElements(this IWebDriver driver, By by, int timeoutInSeconds)
        {
            if (timeoutInSeconds > 0)
            {
                var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
                return wait.Until(drv => (drv.FindElements(by).Count > 0) ? drv.FindElements(by) : null);
            }
            return driver.FindElements(by);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 09:33

    Explicit Wait

    public static  WebDriverWait wait = new WebDriverWait(driver, 60);
    

    Example:

    wait.until(ExpectedConditions.visibilityOfElementLocated(UiprofileCre.UiaddChangeUserLink));
    
    0 讨论(0)
  • 2020-11-22 09:34

    You can find out something like this in C#.

    This is what I used in JUnit - Selenium

    WebDriverWait wait = new WebDriverWait(driver, 100);
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("submit")));
    

    Do import related packages.

    0 讨论(0)
  • 2020-11-22 09:39

    I confused an anonymous function with a predicate. Here's a little helper method:

       WebDriverWait wait;
        private void waitForById(string id)
        {
            if (wait == null)
                wait = new WebDriverWait(driver, new TimeSpan(0, 0, 5));
    
            //wait.Until(driver);
            wait.Until(d => d.FindElement(By.Id(id)));
        }
    
    0 讨论(0)
  • 2020-11-22 09:40

    The clickAndWait command doesn't get converted when you choose the Webdriver format in the Selenium IDE. Here is the workaround. Add the wait line below. Realistically, the problem was the click or event that happened before this one--line 1 in my C# code. But really, just make sure you have a WaitForElement before any action where you're referencing a "By" object.

    HTML code:

    <a href="http://www.google.com">xxxxx</a>
    

    C#/NUnit code:

    driver.FindElement(By.LinkText("z")).Click;
    driver.WaitForElement(By.LinkText("xxxxx"));
    driver.FindElement(By.LinkText("xxxxx")).Click();
    
    0 讨论(0)
  • 2020-11-22 09:41

    Inspired by Loudenvier's solution, here's an extension method that works for all ISearchContext objects, not just IWebDriver, which is a specialization of the former. This method also supports waiting until the element is displayed.

    static class WebDriverExtensions
    {
        /// <summary>
        /// Find an element, waiting until a timeout is reached if necessary.
        /// </summary>
        /// <param name="context">The search context.</param>
        /// <param name="by">Method to find elements.</param>
        /// <param name="timeout">How many seconds to wait.</param>
        /// <param name="displayed">Require the element to be displayed?</param>
        /// <returns>The found element.</returns>
        public static IWebElement FindElement(this ISearchContext context, By by, uint timeout, bool displayed=false)
        {
            var wait = new DefaultWait<ISearchContext>(context);
            wait.Timeout = TimeSpan.FromSeconds(timeout);
            wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
            return wait.Until(ctx => {
                var elem = ctx.FindElement(by);
                if (displayed && !elem.Displayed)
                    return null;
    
                return elem;
            });
        }
    }
    

    Example usage:

    var driver = new FirefoxDriver();
    driver.Navigate().GoToUrl("http://localhost");
    var main = driver.FindElement(By.Id("main"));
    var btn = main.FindElement(By.Id("button"));
    btn.Click();
    var dialog = main.FindElement(By.Id("dialog"), 5, displayed: true);
    Assert.AreEqual("My Dialog", dialog.Text);
    driver.Close();
    
    0 讨论(0)
提交回复
热议问题