Common Selenium Exceptions — Part 1

Eveline D'souza
2 min readMay 11, 2022

Common selenium exceptions and how to handle them!

We have all come across exceptions while implementing tests in our automation frameworks. Our impulsive reaction would be to copy paste the error message and google it, skim through the various blogs and answers on stakeoverflow.

While no doubt, google will always be at our finger-tips but let’s also try to understand and try to remember(if our brain permits!) what does these exceptions mean and when do they occur.

StaleElementReferenceException (every interviewer’s favourite):

This Selenium exception happens if the web element is detached from the current DOM. There may be a case, when an element was in DOM initially but after modifications in the DOM, the element becomes stale or has been deleted and the StaleElementReferenceException is thrown if we make an attempt to access this element.

Common causes:

  • Element is detached from the current DOM (i.e no longer present in the current DOM)
  • Element has been deleted from the DOM. (Page Refresh or user navigated away from the page or element is replaced with identical attributes)

Ways to handle it:

  • Refreshing the page and verifying the element again. (not recommended for webpages which are going to erase out the session data or user entered data upon refresh)
  • Using JavaScript executor to access the element
  • Implement try-catch to access the element in catch block if an exception is thrown from try.
  • Add the explicit wait to wait till the presence of element is detected.

NoSuchElementException:

Webdriver is not able to find and locate elements during runtime, i.e., the FindBy method cannot find a particular component.

Common Causes:

  • Tester may have written incorrect element locator in the findElement(By, by) method.
  • Name of the element may have been changed by the developer.
  • Position of the element may have changed if xpath locator was used to identify the element.
  • Element is taking longer time to load than expected.

Ways to handle it:

  • Fixing the incorrect locator.
  • Adding a wait command.
  • Adding a try catch block along with explicit wait till the presence of the element is detected.

ElementClickInterceptedException:

The command could not be completed as the element receiving the events is concealing the element which was requested clicked.

Common causes:

  • There are overlapping elements.
  • Element hasn’t loaded completely.
  • Element is disabled.
  • Element is not under focus or the action is being performed on the incorrect WebElement
  • Element cannot be located using coordinates on the webpage.

Ways to handle it:

  • Adding waits till the element is clickable.
  • Using JavascriptExecutor for performing the click operation.
  • To click a non-clickable element, first make sure that the overlapping element is closed.
  • In cases where the element needs focus, switch to the actual element and then perform the click action using Action class.
  • If coordinates are used to identify the element, first step should be to maximize the browser window.

Happy Learning !!

--

--

Eveline D'souza

Just an Automation Test Engineer taking notes while learning.