c# - Selenium 等待元素等于某物

标签 c# selenium selenium-webdriver

C#、Winform、Selenium Firefox 网络驱动程序。

基本上我需要等到某个元素等于我程序中的某个元素,这就是我尝试过的

public static string Watchprogress;


Watchprogress = driver.FindElement(By.XPath("//*[@id='watch-toolbar']/aside/div/span")).Text.ToString();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(90)).Until(Watchprogress == "3");

 //And this

 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(90)).Until(By.XPath("//*[@id='watch-toolbar']/aside/div/span")).Text.ToString() == "3");

得到这个错误

无法从用法中推断方法“OpenQA.Selenium.Support.UI.DefaultWait.Until(System.Func)”的类型参数。尝试明确指定类型参数。 5079

对 selenium 还是有点陌生​​,所以我一直在反复试验。

最佳答案

这里有几件事。 Until() 的实现在这里是错误的。您必须在此处使用 ExpectedConditions 或编写自定义函数(见下文)。参见 api

By byXpath = By.XPath("//*[@id='watch-toolbar']/aside/div/span");
IWebElement element =
    new WebDriverWait(_driver, TimeSpan.FromSeconds(90)).Until(ExpectedConditions.ElementExists(byXpath));


if (element.Text.Trim() == "3")
{
    //Pass this
}

LINQ 的另一种选择

string watchprogress = new WebDriverWait(_driver, new TimeSpan(10)).Until(e => e.FindElement(byXpath)).Text.Trim();

if (watchprogress == "3")
{

}

或者

如果您想等到 element 获取文本 3 就可以使用 bool 指示符

bool watchprogress  =
                new WebDriverWait(_driver, new TimeSpan(10)).Until(e => e.FindElement(byXpath)).Text.Trim().Equals("3");

或者

 IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));
 wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));
 //First wait for the page to be completely loaded.
 WebDriverWait wait2 = new WebDriverWait(driver, TimeSpan.FromSeconds(90));
 wait2.IgnoreExceptionTypes(typeof(StaleElementReferenceException));
 wait2.Until(d => d.FindElement(By.XPath("//*[@id='watch-toolbar']/aside/div/span")).Text.Contains("3"));

关于c# - Selenium 等待元素等于某物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28651606/

相关文章:

java - 无法创建新 session 。 appium 代码错误

c# - HttpContext.RemapHandler 是否应该更改哪个处理程序处理请求?

c# - Selenium WebDriver (C#)。将元素拖放到 iframe 中

c# - 如何在 C# 中将鼠标单击事件添加到 Web TextBox

firefox - 升级到 Firefox 18 后,使用 Python 的 Selenium Webdriver 崩溃

python - 使用 python/selenium 样式化类元素

java - Selenium 方法 - maximize() 和 fullscreen() 之间有什么区别

c# - Azure存储表逐行删除键

java - 我想动态更新 gui.properties 文件的键值对

java - Selenium:登录后页面刷新