Selenium等到元素等于某事

时间:2015-02-21 21:32:13

标签: 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

硒仍然有点新鲜,所以我只是在试验和试错。

1 个答案:

答案 0 :(得分:3)

这里有几件事。 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"));