How to analyse 'Element not Found' exceptions when working with Selenium



  • When you are writing Selenium/WebDriver automation scripts you will probably run into the exception NoSuchElementException. You think your selector is working, but somehow the element cannot be found.

    What strategy should I take to analyse different reasons that this might be the case?

    What should I research and how? Good answers would contain example situations and how to resolve them. (e.g. verify selectors, frames, waiting, etc)



  • 1. Trust your code and doubt SUT (Software under test):

    If everything was working fine and the test starts to fail suddenly. Instead of debugging your code for issues, start with checking the actual product. Do some visual inspection and see whether the development team has modified the element or the element is no longer being displayed.

    2. Trust your code and doubt the environment:

    If everything was working fine locally and failed as soon as you integrated to CI/CD. Then investigate the product behaviour in the test server. Mostly due to OS and configuration difference product won't work as it would in local ( Raise a Bug)

    3. Now doubt your scripts (Using absolute XPATH):

    You might be using absolute XPATH, this causes flaky tests when DOM structure changes. Use relative XPATH (CSS would be more recommended). Never use xpath/ if you have a unique ID/name to identify an element.

    4. Now doubt your scripts (Not using explicit wait):

    Sometimes scripts lack explicit wait and try to interact with dynamic elements, this causes the test to fail because it tries to interact with the element before it is even available in DOM.

    5. Now doubt your scripts (Handling spinners):

    Sometimes spinner takes time to get displayed. SO, if you are just checking for the invisibility condition then it will return true and try to interact with next element before the actual spinner event completes

    So, first, check the visibility of spinner and then check for invisibility before interacting with other dynamic elements.

    6. Now doubt your scripts (Not handling iFrames):

    Sometimes element will be inside iframes and scripts won't switch between frames before interacting with these elements.

    Check whether, any parent element contains the tag frame or iframe to determine whether the element is inside an iframe

    *********************Add On******************************************

    7. Now doubt your scripts (Not disabling wait for angular):

    Sometimes products use spinners to wait for asynchronous operations to complete behind the scene. For example, you click login and spinner comes up and will not disappear till the background tasks are not completed.

    In this case, make sure you are not waiting for the asynchronous operations to complete (eg waitforangular flag set to true in protractor) before interacting with the elements in the temporary overlay.

    This is because, if you set waitforangular to true, then scripts wait till all the tasks are completed and by then the temporary overlay (say spinner) will be removed from DOM



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2