【问题标题】:Selenium webdriver c# waiting for text to appearSelenium webdriver c#等待文本出现
【发布时间】:2014-09-22 14:04:48
【问题描述】:

我希望实现的

wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@id='timeLeft']"), "Time left: 7 seconds"));

c# 中的函数,用于等待文本出现。但是,textToBePresentInElementLocated() 仅在 java 中可用。有没有一种简单的方法可以在 c# 中实现这一点,等待文本出现在页面上?

【问题讨论】:

    标签: c# testing selenium selenium-webdriver webdriver


    【解决方案1】:

    我能够通过以下方式解决此问题:

    element = driver.FindElement(By.Xpath("//div[@id='timeLeft']")));
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    wait.Until(ExpectedConditions.TextToBePresentInElement(name, "Time left: 7 seconds"));
    

    【讨论】:

      【解决方案2】:

      Selenium 是开源的,所以看看它的作用:

      https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/support/ui/ExpectedConditions.java#L305

      但是,您拥有 LINQ 的强大功能,所以它会更简单一些,是伪的:

      WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
      wait.IgnoreExceptionTypes(typeof(StaleReferenceException)); // ignore stale exception issues
      wait.Until(d => d.FindElement(By.XPath("//div[@id='timeLeft']")).Text.Contains("Time left: 7 seconds"));
      

      最后一行将等到从d.FindElement(By.XPath("//div[@id='timeLeft']")).Text 返回的文本包含Time left: 7 seconds

      【讨论】:

        【解决方案3】:

        我有我的等待文本出现在元素中的方法的 Java 版本。 也许对你有帮助。

        public void waitForText(WebDriver driver, String text, By selector) {
            try {
                WebDriverWait waitElem = new WebDriverWait(driver, WAIT_TIME);
                waitElem.until(ExpectedConditions.textToBePresentInElementLocated(selector, text));
            }catch (TimeoutException e){
                logger.error(e.toString());
            }
        }
        

        方法采用 webdriver 实例、String to mach text 和 Element 以By Class 格式声明。 WAIT_TIME 是等待时间的常数,以秒为单位。

        【讨论】:

          【解决方案4】:

          等待文本出现

            do
            {
              Thread.Sleep(2000);
            }
          
            while(!driver.FindElement(ByXpath("//The Xpath of the TEXT//")).Displayed);
          
            {
          
            }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-10-23
            • 2014-04-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-12-03
            相关资源
            最近更新 更多