kinda confused. I have a method I created to help me handle various scenarios

 public void WaitHandler(IWebElement element)
            int i = 0;
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));

            while (i < 10)
                    wait.Until(driver => element.Displayed);
                catch (Exception)

The problem im having is when i call this method and pass in my webelement, in 99% of scenarios it handles any exception I come across, i mainly use this for stale element exceptions but sometimes itll throw a NoSuchElementException almost immediately and not wait for the element to load.

I have the timeout set to 15 seconds, but when it gets to the line in question it fails right away not even bothering to wait 15 seconds.


I noticed if i use a try catch outside of this method in the problem code that try catch works, but why does the try catch fail when i use it in this calling method and only for this specific exception?

Can confirm its NoSuchElementException not a time out exception(shouldnt matter cause im catching Exception) even added

catch (OpenQA.Selenium.NoSuchElementException) 

to no luck. Weird problem I know.

  • You must check the stacktrace if the exception trigger happens inside your WaitHandler. It might happened the moment you look for the element which is prior to passing the element to waitHandler. Aug 2 at 2:26
  • I'm not familiar with the language, but in all languages that I've developed across different tech stacks, you need to assign a variable to the catch, like try{ my.methd( x ); } catch (Exception ex) { print( ex.getMessage() ); } - the ex is therefore the Exception variable that is essentially the container of the Exception in my example. Aug 4 at 14:23
  • Do you have any implicit wait define in your project?
    – Sachintha
    Aug 10 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.