2014-09-22 50 views
0

我希望实现在C#中硒的webdriver C#等文字出现

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

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

回答

1

硒是开源的,所以看看它做什么:

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

0

我有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对于以秒为单位的等待时间是恒定的。

-2

要等待文本出现

do 
    { 
    Thread.Sleep(2000); 
    } 

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

    { 

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