【问题标题】:Explicit waits in Selenium C# doesn't work . What is wrong?Selenium C# 中的显式等待不起作用。怎么了?
【发布时间】:2017-05-08 06:47:12
【问题描述】:

所以我遇到了显式等待的问题。我不想使用 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));
        }



    }
}

【问题讨论】:

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


    【解决方案1】:

    我用 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();
    

    【讨论】:

    • 我会这样做,但现在我正在尝试根据我的解决方案调整您的答案,但目前我没有成功。
    • 您的回答帮助了我。这对我来说是唯一有用的行:wait.Until(ExpectedConditions.ElementToBeClickable(webElement)).Click();
    • 太好了:)
    • 请给我您的联系方式、电子邮件或其他信息。
    猜你喜欢
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 2020-02-29
    • 1970-01-01
    相关资源
    最近更新 更多