我希望实现
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@id='timeLeft']"), "Time left: 7 seconds"));
c#中的函数,等待文本出现。但是,textToBePresentInElementLocated()仅在Java中可用。有没有一种简单的方法可以在c#中实现这一点,等待文本出现在页面上?
textToBePresentInElementLocated()
Selenium是开源的,因此请看一下它的作用:
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']")).Textcontains 返回的文本为止Time left: 7 seconds。
d.FindElement(By.XPath("//div[@id='timeLeft']")).Text
Time left: 7 seconds