使用C#Selenium Webdriver时忽略异常等待wait.untill()函数 [英] Ignoring exceptions when using c# selenium webdriverWait wait.untill() function
问题描述
为了检查某个元素是否存在并单击,我试图编写一个布尔方法,该方法将等待使用C#硒的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等待=新的webDriverWait(驱动程序,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实现包含公共void IgnoreExceptionTypes(params Type [] exceptionTypes)方法的DefaultWait类。
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 Webdriver时忽略异常等待wait.untill()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!