问题描述
我希望达到的
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@id='timeLeft']"), "Time left: 7 seconds"));
c# 中的函数,用于等待文本出现.但是,textToBePresentInElementLocated()
仅在 java 中可用.有没有一种简单的方法可以在 c# 中实现这一点,等待文本出现在页面上?
function in c#, to wait for a text to appear. However, the textToBePresentInElementLocated()
is only available in java. Is there a simple way to achieve this in c# , waiting for text to appear on page?
推荐答案
Selenium 是开源的,所以看看它的作用:
Selenium is open source, so take a look at what it does:
然而,您拥有 LINQ 的强大功能,所以它会更简单一些,伪:
You have the power of LINQ however, so it's going to be a little simpler, pseudo:
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 返回的文本左:7 秒
.
The last line will wait until the text returned from d.FindElement(By.XPath("//div[@id='timeLeft']")).Text
contains Time left: 7 seconds
.
这篇关于Selenium webdriver c# 等待文本出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!