'Selenium C# DefaultWait IgnoreExceptionTypes does not work

I'm using DefaultWait while waiting for an WebElement to be clickable. Although TargetInvocationException is one of the exceptions in my list of exceptions to be ingnored during waiting, I still have tests failing with this exception before TimeOut period is reached. This is not what I expected.

public static void WaitAndClick(this IWebDriver driver, IWebElement webelement)
    {

        DefaultWait<IWebDriver> fluentWait = new DefaultWait<IWebDriver>(driver)
        {
            Timeout = TimeSpan.FromSeconds(Configuration.WaitTime.TotalSeconds),
            PollingInterval = TimeSpan.FromMilliseconds(500)
        };
        fluentWait.IgnoreExceptionTypes(typeof(TargetInvocationException),typeof(NoSuchElementException),typeof(InvalidOperationException));
        fluentWait.Until(ExpectedConditions.ElementToBeClickable(webelement));
            webelement.Click();

    }


Solution 1:[1]

Try using WebDriverWait instead of DefaultWait<IWebDriver>, which is basically the same thing.

public static void WaitAndClick(this IWebDriver driver, IWebElement webelement)
{
    WebDriverWait fluentWait = new WebDriverWait(driver,Configuration.WaitTime.TotalSeconds);
    //No need to set PollingInterval, default is already 500ms
    fluentWait.IgnoreExceptionTypes(typeof(TargetInvocationException),typeof(NoSuchElementException),typeof(InvalidOperationException));
    fluentWait.Until(ExpectedConditions.ElementToBeClickable(webelement));

    webelement.Click();
}

I see no need to use Interface when there is a predefined concrete class for exactly the same reason (waiting with the webDriver). Report back if the problem still persists.

Update: if it does not solve your problem, use lamba expression to deligate the function needed for Until() (public TResult Until<TResult>(Func<T, TResult> condition);)

        fluentWait.Until(driver =>
        {
            try
            {
                driver.FindElement(/*By Locator*/).Click();
            }
            catch (Exception ex)
            {
                Type exType = ex.GetType();
                if (exType == typeof(TargetInvocationException) ||
                    exType == typeof(NoSuchElementException) ||
                    exType == typeof(InvalidOperationException))
                {
                    return false; //By returning false, wait will still rerun the func.
                }
                else
                {
                    throw; //Rethrow exception if it's not ignore type.
                }
            }

            return true;
        });

Solution 2:[2]

Thanks to Thodoris Koskinopoulos, this solution is working fine for me:

    public static void WaitAndClick(this IWebDriver driver, IWebElement webelement)
    {
        var fluentWait = new WebDriverWait(driver, Configuration.WaitTime);
        fluentWait.Until(webDriver =>
        {
            try
            {
                webelement.Click();
            }
            catch (Exception ex)
            {
                if (ex is TargetInvocationException ||
                    ex is NoSuchElementException ||
                    ex is InvalidOperationException)
                {
                    return false; //By returning false, wait will still rerun the func.
                }
                {
                    throw; //Rethrow exception if it's not ignore type.
                }
            }
            return true;
        });
    }   

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2