Selenium webdriver c# 等待文本出现 [英] Selenium webdriver c# waiting for text to appear

查看:33
本文介绍了Selenium webdriver c# 等待文本出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望达到的

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:

https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/support/ui/ExpectedConditions.java#L305

然而,您拥有 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# 等待文本出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆