我希望实现在C#中硒的webdriver C#等文字出现
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@id='timeLeft']"), "Time left: 7 seconds"));
功能,等待出现一个文本。但是,textToBePresentInElementLocated()
仅适用于java。有没有一种简单的方法在c#中实现这一点,等待文本出现在页面上?
我希望实现在C#中硒的webdriver C#等文字出现
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@id='timeLeft']"), "Time left: 7 seconds"));
功能,等待出现一个文本。但是,textToBePresentInElementLocated()
仅适用于java。有没有一种简单的方法在c#中实现这一点,等待文本出现在页面上?
硒是开源的,所以看看它做什么:
你有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
。
我有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的实例,字符串马赫文字和元素By Class格式声明。 WAIT_TIME对于以秒为单位的等待时间是恒定的。
要等待文本出现
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"));