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