使用C#Selenium Webdriver时忽略异常等待wait.untill()函数

Ita*_*med 6 .net c# selenium selenium-webdriver visual-studio-2013

为了检查是否存在Element和clickble,我尝试编写一个布尔方法,该方法将等待使用C#硒的webDriverWait启用和显示该元素,如下所示:

webDriverWait等待=新的webDriverWait(驱动程序,timeSpan.fromSeconds(60));

Wait.untill(d => webElement.enabled()&& webElement.displayed());

如果以上条件没有发生,我希望该方法返回“ false”。问题是我抛出了异常。如果抛出异常,如何忽略诸如noSuchElementException和timeOutException之类的异常?我尝试使用try catch块,但是它没有帮助,并且引发了异常。

小智 6

WebDriverWait实现DefaultWait类,该类包含公共void IgnoreExceptionTypes(params Type [] exceptionTypes)方法。

您可以使用此方法来定义在单击之前等待元素启用时要忽略的所有异常类型。

例如 :

WebDriverWait wdw = new WebDriverWait(driver, TimeSpan.FromSeconds(120));
wdw.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(ElementNotVisibleException));
Run Code Online (Sandbox Code Playgroud)

在前面的代码中,wait将忽略NoSuchElementException和ElementNotVisibleException异常

  • 没有。我正在使用这个确切的代码,但它仍然抛出一个异常“没有这样的元素,无法找到元素...[等等]...”Grrrr..... (2认同)