Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IgnoreExceptionTypes does not work (C# Webdriver)

I have found that in C# whether using the WebDriverWait class or the DefaultWait class, in either case the IgnoreExceptionTypes method appears not to work.

I.e. in either case when running against my page a StaleElementReferenceException is thrown despite the fact I am instructing the code to ignore these exceptions.

WebDriverWait example :

public void WaitElementToBeClickable(IWebElement element)
    {
        var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(60));
        wait.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(StaleElementReferenceException));
        wait.Until(ExpectedConditions.ElementToBeClickable(element));
    }

DefaultWait example :

public IWebElement SafeWaitForDisplayed(IWebElement webElement) {

    var w = new DefaultWait<IWebElement>(webElement);
            w.Timeout = TimeSpan.FromSeconds(30);
            w.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(StaleElementReferenceException));
            return w.Until(ctx =>
            {
                var elem = webElement;
                if (elem.Displayed)
                    return elem;
                else
                    return null;
            });
    }

Any suggestions gratefully received. There appears to be very little on the web about usage of this particular method and others have found it not to work also with no workarounds suggested.

like image 832
hjr2000 Avatar asked Jul 30 '15 10:07

hjr2000


1 Answers

The IgnoreExceptionTypes will only last throughout the wait until it times out. I am using the DefaultWait and like you was expecting it to return null. It does not. When the timeout is reached it will throw the exception. Consequently I have enclosed it in a try catch to handle the exception appropriately on timeout.

like image 193
jibbs Avatar answered Sep 21 '22 11:09

jibbs