其他分享
首页 > 其他分享> > 硒等待元素相等

硒等待元素相等

作者:互联网

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"));

标签:selenium,selenium-webdriver,c
来源: https://codeday.me/bug/20191120/2045957.html