微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

硒等待元素相等

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

硒仍然有点新,所以我一直在尝试和尝试.

解决方法:

这里有几件事.在这里,直到()的实现是错误的.您必须在这里使用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")
{

}

要么

简单地说,如果您要等到元素获得文本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"));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐