使用 c# selenium webdriverWait wait.untill() 函数时忽略异常 [英] Ignoring exceptions when using c# selenium webdriverWait wait.untill() function
问题描述
为了检查元素是否存在并点击,我正在尝试编写一个布尔方法,该方法将使用 C# selenium 的 webDriverWait 等待元素被启用和显示,如下所示:
In order to check if an Element is exists and clickble i'm trying to write a boolean method which will wait for the element to be enabled and displyed using C# selenium's webDriverWait as follow:
webDriverWait wait = new webDriverWait(driver, timeSpan.fromSeconds(60));
webDriverWait wait = new webDriverWait(driver, timeSpan.fromSeconds(60));
Wait.untill(d => webElement.enabled() && webElement.displayed());
Wait.untill( d => webElement.enabled() && webElement.displayed());
如果上述条件不发生,我希望该方法返回false".问题是我抛出了异常.如果抛出异常,如何忽略诸如 noSuchElementException 和 timeOutException 之类的异常?我曾尝试使用 try catch 块,但它没有帮助并抛出异常.
In case the above conditions do not happen, I want the method to return 'false'. The problem is that I get exceptions thrown. How can I ignore exceptions such as noSuchElementException and timeOutException in case they are thrown? I have tried to use try catch block but it didn't help and exceptions were thrown.
推荐答案
WebDriverWait 实现 DefaultWait 类,该类包含 public void IgnoreExceptionTypes(params Type[] exceptionTypes) 方法.
WebDriverWait implements DefaultWait class that contains public void IgnoreExceptionTypes(params Type[] exceptionTypes) method.
您可以使用此方法定义在单击之前等待元素启用时要忽略的所有异常类型.
You can use this method for defining all the exception types you want to ignore while waiting for element to get enabled before clicking.
例如:
WebDriverWait wdw = new WebDriverWait(driver, TimeSpan.FromSeconds(120));
wdw.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(ElementNotVisibleException));
在前面的代码中wait会忽略NoSuchElementException和ElementNotVisibleException异常
In the preceding code wait will ignore NoSuchElementException and ElementNotVisibleException exceptions
这篇关于使用 c# selenium webdriverWait wait.untill() 函数时忽略异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!