我在等待 WebElement 可单击时使用 DefaultWait。尽管 TargetIncationException 是等待期间要忽略的异常列表中的异常之一,但在达到超时期限之前,我仍然有测试因该异常而失败。这不是我所期望的。
public static void WaitAndClick(this IWebDriver driver, IWebElement webelement)
{
DefaultWait<IWebDriver> fluentWait = new DefaultWait<IWebDriver>(driver)
{
Timeout = TimeSpan.FromSeconds(Configuration.WaitTime.TotalSeconds),
PollingInterval = TimeSpan.FromMilliseconds(500)
};
fluentWait.IgnoreExceptionTypes(typeof(TargetInvocationException),typeof(NoSuchElementException),typeof(InvalidOperationException));
fluentWait.Until(ExpectedConditions.ElementToBeClickable(webelement));
webelement.Click();
}
尝试使用WebDriverWait
代替DefaultWait<IWebDriver>
,这基本上是同一件事。
public static void WaitAndClick(this IWebDriver driver, IWebElement webelement)
{
WebDriverWait fluentWait = new WebDriverWait(driver,Configuration.WaitTime.TotalSeconds);
//No need to set PollingInterval, default is already 500ms
fluentWait.IgnoreExceptionTypes(typeof(TargetInvocationException),typeof(NoSuchElementException),typeof(InvalidOperationException));
fluentWait.Until(ExpectedConditions.ElementToBeClickable(webelement));
webelement.Click();
}
当出于完全相同的原因(等待 webDriver)存在预定义的具体类时,我认为不需要使用 Interface。如果问题仍然存在,请报告。
更新:如果它不能解决您的问题,请使用 lambda 表达式来委托 Until() 所需的函数(public TResult Until<TResult>(Func<T, TResult> condition);
)
fluentWait.Until(driver =>
{
try
{
driver.FindElement(/*By Locator*/).Click();
}
catch (Exception ex)
{
Type exType = ex.GetType();
if (exType == typeof(TargetInvocationException) ||
exType == typeof(NoSuchElementException) ||
exType == typeof(InvalidOperationException))
{
return false; //By returning false, wait will still rerun the func.
}
else
{
throw; //Rethrow exception if it's not ignore type.
}
}
return true;
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)