如何显式等待IWebElement .Text属性中存在任何文本
/// <summary>
/// Work
/// </summary>
/// <param name="by"></param>
/// <returns></returns>
public IWebElement WaitUntilAnyTextIsPresentInElementValue(By by)
{
IWebElement element = _driver.FindElement(by);
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(30));
wait.Until(d => d.FindElement(by).GetAttribute("value") != string.Empty);
return element;
}
/// <summary>
/// Don't Work
/// </summary>
/// <param name="by"></param>
/// <returns></returns>
public IWebElement WaitUntilAnyTextIsPresentInElementText(By by)
{
IWebElement element = _driver.FindElement(by);
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(30));
wait.Until(d => d.FindElement(by).Text != string.Empty);
return element;
}
第一种方法工作正常,第二种方法不等......
IWebElement dsfTest = WaitUntilAnyTextIsPresentInElementValue(By.Id("dsfTest"));
//It wait 30 seconds, if text is found try to Assert, if not throw TimeOutException as expected...
Assert.AreEqual("Test", dsfTest.Text);
IWebElement dsfTest = WaitUntilAnyTextIsPresentInElementText(By.Id("dsfTest"));
//Try to assert without wait (sometimes it will work)... (i know that it doesn't wait because it don't throw TimeOutException and Assert fail because text still empty)
Assert.AreEqual("Test", dsfTest.Text);
............................................... .................................
这一项工作,但需要传递一个特定的文字来观察......
public IWebElement WaitUntilTextIsPresentInElementText(By by, string text)
{
IWebElement element = _driver.FindElement(by);
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.TextToBePresentInElement(element, text));
return element;
}