当前位置: 首页 > 知识库问答 >
问题:

Selenium C#中的显式等待不起作用。什么是错的?

端木澄邈
2023-03-14

所以我有显式等待的问题。我不想用线。睡眠()。这是一个简单的html" target="_blank">测试,它打开一个页面,然后来回移动。加载此页面大约需要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));
        }



    }
}

共有1个答案

杨选
2023-03-14

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

代码所做的是:在20秒内,它每500ms检查一次元素是否出现在页面上。如果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();
 类似资料:
  • 我正在尝试与selenium webDrive同步,但某些东西无法隐含地使用等待()。 我隐含理解的方式等待(...)是代码正在等待,直到元素在最大时间内可用。 以下代码因错误而崩溃: 系统。out ist打印:-- 我也用Geckodriver试过了,结果也一样。 我也增加了等待时间,但结果相同。 使其工作的唯一方法是使用Thread.sleep()(上面评论) 编辑请注意,我没有看到任何重复与

  • 问题内容: 我正在学习Java Maven Selenium。我想要在Selenium中使用这样的东西。 打开网站(例如https://www.facebook.com) 单击登录的电子邮件字段 等待20秒 输入我的电子邮件 这是我的简单代码: 该代码不起作用。它只会打开Facebook,单击电子邮件字段并输入我的电子邮件ID,而不是等待10秒钟才输入我的电子邮件。 问题答案: 并且无法正常工作,

  • 问题内容: 这是我第一次使用selenium和无头浏览器,因为我想使用ajax技术来爬网某些网页。 效果很好,但是在某些情况下,加载整个页面会花费太多时间(尤其是当某些资源不可用时),因此我必须为selenium设置超时时间。 首先,我尝试了和,但是当我设置这些超时时,如果页面未完全加载,我将不会得到任何页面源,如下代码所示: 所以我尝试使用隐式等待和条件等待,如下所示: 这次我得到了想要的内容。

  • 问题内容: 我正在学习Java Maven Selenium。我希望在Selenium中使用。 打开网站(例如https://www.facebook.com) 单击登录的电子邮件字段 等待20秒 输入我的电子邮件 这是我的简单代码: 该代码不起作用。它只会打开Facebook,单击电子邮件字段并输入我的电子邮件ID,而不是等待10秒钟才输入我的电子邮件。 问题答案: 并且无法正常工作,他们将在指

  • 问题内容: Selenium WebDriver中有隐式和显式等待。它们之间有什么区别? 请分享有关Selenium WebDriver的知识。请显示带有隐式和显式等待的实时示例。 问题答案: 检查以下链接: -通过轮询DOM来指示Web驱动程序等待一段时间。声明隐式等待后,它将在Web驱动程序实例的整个生命周期中都可用。默认情况下,该值将为0。如果设置了更长的默认值,则该行为将根据浏览器/驱动程

  • 问题内容: 我是Selenium WebDriver的新手,正在尝试了解“等待”元素出现的正确方法。 我正在测试一个带有单选按钮答案的问题的页面。选择答案时,JavaScript可能启用/禁用页面上的某些问题。 问题似乎是Selenium的“单击速度太快”,而不是等待Javascript完成。我尝试用两种方式解决此问题- 显式等待解决了该问题。具体来说,这可以解决我的问题: 但是,我 宁愿 使用隐