我在等待 WebElement 可点击时使用 DefaultWait。尽管 TargetInvocationException 是我在等待期间要忽略的异常列表中的异常之一,但在达到超时期限之前,我仍然有测试因此异常而失败。这不是我所期望的。
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();
}
Run Code Online (Sandbox Code Playgroud)
尝试使用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();
}
Run Code Online (Sandbox Code Playgroud)
当出于完全相同的原因(等待 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;
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2061 次 |
| 最近记录: |