【问题标题】: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"));
【解决方案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);
{
}