C# – Ignoring exceptions when using c# selenium webdriverWait wait.untill() function

cnetseleniumselenium-webdrivervisual-studio-2013

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));

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

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.

Best Answer

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.

For example :

WebDriverWait wdw = new WebDriverWait(driver, TimeSpan.FromSeconds(120));
wdw.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(ElementNotVisibleException));

In the preceding code wait will ignore NoSuchElementException and ElementNotVisibleException exceptions

Related Topic