How to Handle Selenium Exception with Code?

🚀 What is an Exception?

  • The unwanted event which occurs during the execution of a program or code;
  • It disturbs the normal flow of the program.
  • Example: You are trying 1/0 division.
  • Arithmetic Exception trying to access an element of an array which is more than the size of that array
  • ArrayOutOfBound Exception handling is a very important part of every Selenium Script and for Interview.

🚀 Top 10 Selenium Exceptions

  • NotFoundException
  • NoSuchWindowException Webdriver is trying to switch to an invalid window, which is unavailable
  • NoSuchFrameException: Webdriver attempts to switch to an invalid frame, which is unavailable
  • NoSuchElementException Webdriver is not able to determine the elements during runtime, i.e., the FindBy method cannot find a particular component
  • NoAlertPresentException Webdriver is trying to switch to an invalid alert, which is unavailable
  • TimeoutException The command did not complete in the specified time. For example, the element didn’t display at the specified time. This is especially encountered when working with waits
  • StaleElementReferenceException The referenced element is no longer present on the DOM page (a reference to a component is now Stale). For example, the item belongs to a different frame than the current one or the user has navigated away to another page Due to ? Navigation to another page DOM has refreshed A frame or window switch
  • InvalidElementState
  • ElementNotVisibleException In spite of the element being present in the DOM, it is not visible (can not be interactive). For example, elements defined in HTML with type =”hidden”
  • ElementNotSelectableException
    An element is disabled (can not be clicked/selected) in spite of being present in the DOM
    For ElementNot Interactable
  • ExpectedConditions.visibilityOfElementLocated(“Passwd”))); ExpectedConditions.elementToBeClickable Javascript clicks
  • SessionNotFoundException
  • Webdriver is acting immediately after ‘quitting’ the browse



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store