简体   繁体   English

C#硒隐式等待

[英]C# Selenium Implicit Wait

I'm using selenium/firefox/c# to enter data into several fields in a webpage, and then submit it to the website by clicking a submit button. 我正在使用selenium / firefox / c#在网页的多个字段中输入数据,然后通过单击“提交”按钮将其提交到网站。 I have tried using ImplicitlyWait so that the program waits for the results page to load for a maximum of 45 seconds and then grab results from that page. 我尝试使用ImplicitlyWait以便程序等待结果页面加载最多45秒,然后从该页面获取结果。 I have it currently coded as this 我目前有这样编码

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(45));
driver.FindElement(By.Id("dnn_ctr1734_Professional_btnSubmit")).Click();

I'm putting the implicit wait before clicking the submit button, but even if it takes only 15 seconds for the page to load its not grabbing the results when it finally loads. 我要在单击“提交”按钮之前进行隐式等待,但是即使页面仅花费15秒来加载页面,也无法在最终加载时获取结果。 Do I have the ImplicitWait in the right order? 我有正确顺序的ImplicitWait吗?

The correct place to put an implicit wait is usually right after you initialize the driver. 通常,在初始化驱动程序之后,才可以正确地放置隐式等待。 It basically acts as a default wait time for an action to complete. 基本上,它充当操作完成的默认等待时间。

There are several ways to solve your problem... 有几种方法可以解决您的问题...

  1. Use an Explicit wait with an expected condition to wait until your busy spinner is gone. 使用预期条件进行显式等待,直到忙碌的微调器消失。

    var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10)); var wait = new WebDriverWait(Driver.Instance,TimeSpan.FromSeconds(10)); wait.Until(driver => !driver.FindElement(By.Id("busySpinnerID")).Displayed); wait.Until(driver =>!driver.FindElement(By.Id(“ busySpinnerID”))。Displayed);

  2. Wait until a particular element is visible on the page 等待直到页面上显示特定元素

    var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10)); var wait = new WebDriverWait(Driver.Instance,TimeSpan.FromSeconds(10)); wait.Until(driver => driver.FindElement(By.Id("knownElementId")).Displayed); wait.Until(driver => driver.FindElement(By.Id(“ knownElementId”))。显示);

  3. Use JavaScript to wait until the page has loaded 使用JavaScript等待页面加载完毕

    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30)); var wait = new WebDriverWait(driver,TimeSpan.FromSeconds(30)); wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete")); wait.Until(driver1 =>(((IJavaScriptExecutor)driver).ExecuteScript(“ return document.readyState”)。Equals(“ complete”)));

You should use explicit wait, use code like below: 您应该使用显式等待,使用如下代码:

 new WebDriverWait(driver, TimeSpan.FromSeconds(45)).Until(ExpectedConditions.ElementExists((By.Id("dnn_ctr1734_Professional_btnSubmit"))));
 driver.FindElement(By.Id("dnn_ctr1734_Professional_btnSubmit")).Click();

I agree with Dmitry, the best way to deal with waits is with the explicit wait function. 我同意Dmitry的观点,处理等待的最佳方法是使用显式等待功能。 To make your life even easier, you can add this into an extension method so you're always going to be waiting for the element to exist before clicking. 为了使您的生活更加轻松,可以将其添加到扩展方法中,以便始终在单击之前等待该元素存在。 Example: 例:

Class Actions {

        public static IWebDriver ClickOn(IWebDriver driver, string button) 
        {
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
            wait.Until(ExpectedConditions.ElementExists(By.XPath(button)));
            driver.FindElement(By.XPath(button)).Click();
            return driver;
        }
}

Then your code can be as simple as 然后,您的代码可以像

Actions.ClickOn(d, SignInButton);

And your code will still wait for the element to exist before clicking. 并且您的代码仍将等待元素存在,然后单击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM