Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium webdriver c#等待文本出现_C#_Testing_Selenium_Selenium Webdriver_Webdriver - Fatal编程技术网

Selenium webdriver c#等待文本出现

Selenium webdriver c#等待文本出现,c#,testing,selenium,selenium-webdriver,webdriver,C#,Testing,Selenium,Selenium Webdriver,Webdriver,我希望达到目标 wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@id='timeLeft']"), "Time left: 7 seconds")); 函数,以等待文本出现。但是,texttobepresentelementlocated()仅在java中可用。有没有一种简单的方法可以在c#中实现这一点,即等待文本出现在页面上?Selenium是开源的,所以请看一下它的功能: 但是,

我希望达到目标

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

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

Selenium是开源的,所以请看一下它的功能:

但是,您拥有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']”)返回的文本。文本
包含
剩余时间:7秒

我有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实例、字符串到mach文本以及以格式声明的元素。
等待时间对于以秒为单位的等待时间是恒定的。

等待文本出现

  do
  {
    Thread.Sleep(2000);
  }

  while(!driver.FindElement(ByXpath("//The Xpath of the TEXT//")).Displayed);

  {

  }

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

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"));