我得到了他的代码,直到找到控件为止
WebDriverWait wait = new WebDriverWait(Browser, TimeSpan.FromSeconds(20));
var ok = wait.Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.XPath("(//button[@id='btnOkDialog'])[" + buttonOccuranceNo + "]")));
在发生任何事件后在dom中进行某些控制时,我将使用此功能。
现在,如果我缩小页面的搜索范围[就像在WebDriver内将div作为WebElement一样],那么如何搜索该范围内的任何控件,直到找到或超时?
您需要使用DefaultWait
该类来设置其他等待:
var waitInnerScope = new DefaultWait<IWebElement>(divInsideWebDriver);
waitInnerScope.Timeout = TimeSpan.FromSeconds(timeout);
waitInnerScope.IgnoreExceptionTypes(typeof(NoSuchElementException));
然后使用wait.Until
:
var ok = waitInnerScope.Until(PresenceOfAllElementsLocatedBy(By.XPath("(//button[@id='btnOkDialog'])[" + buttonOccuranceNo + "]")));
编辑:由于ExpectedConditions
包含实现Func<IWebDriver, ...>
与结合使用的静态方法WebDriverWait
,因此您必须使用自己的方法,其用法IWebElement
如下:
public static Func<IWebElement, ReadOnlyCollection<IWebElement>> PresenceOfAllElementsLocatedBy(By locator)
{
return (element) =>
{
try
{
var elements = element.FindElements(locator);
return elements.Any() ? elements : null;
}
catch (StaleElementReferenceException)
{
return null;
}
};
}
或者,您可以将lambda表达式用于匿名方法,如下所示:
waitInnerScope.Until<ReadOnlyCollection<IWebElement>>((element) =>
{
try
{
var elements = element.FindElements(By.XPath("(//button[@id='btnOkDialog'])[" + buttonOccuranceNo + "]"));
return elements.Any() ? elements : null;
}
catch (StaleElementReferenceException)
{
return null;
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句