c# - Selenium C# 中的显式等待不起作用。怎么了?

标签 c# selenium selenium-webdriver automated-tests selenium-chromedriver

所以我遇到了显式等待的问题。我不想使用 Thread.Sleep()。这是一个简单的测试,它打开一个页面然后返回和前进。加载此页面大约需要 2-3 秒,我想以动态方式(测试)执行此操作。希望我不会太困惑。我做了很多研究,但没有任何效果,也许我做错了什么。 (我正在使用 Resharper 运行单元测试)

这里我也有解决办法:

https://www.dropbox.com/s/1k5vjc5czvmdd3u/ConsoleApplication2.rar?dl=0

我正在使用 FindElement 方法的扩展,因此我相信调用此方法并自行等待会很容易。我在解决方案中评论了一些解释。如果有人给我一些帮助,我将不胜感激。 (抱歉,英语不是很完美)。

using System;
using System.Threading;
using ConsoleApplication2.Extensions;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

namespace ConsoleApplication2
{
    class UnitTest1
    {
        private IWebDriver driver = new ChromeDriver();
        [SetUp]
        public void Initialize()
        { 
            driver.Url = "some url";
            Thread.Sleep(3500);
           // driver.Manage().Timeouts().ImplicitlyWait(TimeSpan(20));

        }

       [Test]
        public void CheckBackForward()
        {
            //Go to first page in Online Help
            driver.FindElement(By.XPath("//span/ul/li/span/a")).Click();
            // So if I am commenting this Thread.Sleep
            // it will throw an exception at the extension method at line 13 at "@by"
            // I've also checked this without extension method but still the same problem. It doesn'w wait at all, it will throw this 
            // exception as soon as FindElement method is called.
            // I know that I shouldn't mix explicit waits with implicit ones.
            //Thread.Sleep(1500);
            //Store title
            var title_text = driver.FindElement(By.XPath("//div[@id='shellAreaContent']/div/div[2]/ol/li[3]/span"),60).Text;
            //Check if Back is enabled
            Assert.IsTrue(driver.FindElement(By.XPath("//div[3]/a/span")).Enabled);
            //Go back
            driver.FindElement(By.XPath("//div[3]/a/span")).Click();
            //Check if Forward is enabled
            Assert.IsTrue(driver.FindElement(By.XPath("//div[4]/a/span")).Enabled);
            //Go forward
            driver.FindElement(By.XPath("//div[4]/a/span")).Click();
            //Store title
            var title_text2 = driver.FindElement(By.XPath("//div[@id='shellAreaContent']/div/div[2]/ol/li[3]/span")).Text;
            //Check if you are on the same page
            Assert.AreEqual(title_text2,title_text);
        }



        [TearDown]
        public void EndTest()
        {
            driver.Quit();
        }

    }
}

这是扩展:

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

namespace ConsoleApplication2.Extensions
{
    public static class Extension
    {
        public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds)
        {
            if (timeoutInSeconds <= 0) return driver.FindElement(@by);
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
            return wait.Until(drv => drv.FindElement(@by));
        }



    }
}

最佳答案

我已经使用 Selenium 编码 6 个多月了,我遇到了和你一样的问题。我创建了这个扩展方法,它每次都对我有用。

代码的作用是: 在 20 秒内,它每 500 毫秒检查一次该元素是否出现在页面上。如果 20 秒后仍未找到,则会抛出异常。 这将帮助您进行动态等待。

  public static class SeleniumExtensionMethods {
      public static WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
      public static void SafeClick(this IWebElement webElement) {
          try {
              wait.Until(ExpectedConditions.ElementToBeClickable(webElement)).Click();
          } catch (TargetInvocationException ex) {
              Console.WriteLine(ex.InnerException);
          }

      }

然后替换你的这段代码:

driver.FindElement(By.XPath("//span/ul/li/span/a")).Click();

与:

IWebElement x = driver.FindElement(By.XPath("//span/ul/li/span/a"));
x.SafeClick();

关于c# - Selenium C# 中的显式等待不起作用。怎么了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41286593/

相关文章:

c# - WPF.NET : OpenSubkey() don't find value in registry

c# - 使用特定方法套接字服务器调用

c# - 如何压缩文件夹中的所有文件

python - 元素不可见异常 : element not visible Python

c# - 如何在出错时重新启动嵌入式 NETMF C# 程序?

css - Selenium Webdriver中如何使用css或xpath获取验证码

javascript - 如何点击没有固定类别值的按钮?但它有一个固定的 'data-testid'

java - 使用 Webdriver 确定页面是否已更改

selenium-webdriver - 设置 Jasmine + Webdriver - "cannot use import outside a module"

javascript - Selenium 清除文本并将文本输入到 textarea 文本框中 [Python]