一,如果普通的获取元素,是没问题的(这个说明是配置没问题的吧)。
二,我故意写了个“n个a”的不存在的元素,测试显示等待,设置的时间是180秒,可是刚不到10秒就报错,找不到元素。
查了很多资料也都没解决,谢谢呀。
是不是这些问题导致的,我运行起来有这个报错。
我看了一下WebDriver的源码,可以看到,如果未找到元素,会抛出一个 NoSuchElementException
internal IWebElement GetElementFromResponse(Response response)
{
if (response == null)
{
throw new NoSuchElementException();
}
WebElement element = null;
Dictionary<string, object> elementDictionary = response.Value as Dictionary<string, object>;
if (elementDictionary > null)
{
element = this.elementFactory.CreateElement(elementDictionary);
}
return element;
}
再看到Until的源码
public virtual TResult Until<TResult>(Func<T, TResult> condition, CancellationToken token)
{
....
while (true)
{
try
{
TResult val = condition(input);
...
}
catch (Exception ex)
{
if (!IsIgnoredException(ex))
{
throw;
}
lastException = ex;
}
...
}
}
这里这个IsIgnoredException
就会忽略你在wait.IgnoreExceptionTypes
函数添加的异常类型。
所以你这里显示这个异常,不是程序报错,是Visual Studio调试工具的提醒。
我将测试代码改成如下:
wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
try
{
var ele = wait.Until(bb =>
bb.FindElement(By.Id("aaaaaaaaaa"))
);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
直接运行编译后的程序,只会收到一个超时的错误,并不会收到NoSuchElementException
的错误