Top 5 commonly encountered exceptions in Python Selenium WebDriver

The Blog To Learn Selenium and Test Automation

Top 5 commonly encountered exceptions in Python Selenium WebDriver

NoSuchElementException

selenium.common.exceptions.NoSuchElementException

Base: selenium.common.exceptions.WebDriverException

This exception can be imported as ‘from selenium.common.exceptions import NoSuchElementException’

This exception is raised or thrown when element could not be found.

There could many possible reasons for this exception to occur

  • Element locator is not correct
  • Page is not yet loaded
  • Element is not yet displayed on the screen at the time of the find operation

StaleElementReferenceException

selenium.common.exceptions.StaleElementReferenceException

Base: selenium.common.exceptions.WebDriverException

This exception can be imported as ‘from selenium.common.exceptions import StaleElementReferenceException’

This exception is raised or thrown when a reference to an element is now “stale” – means that the element was available at the time of find operation but no longer appears on the DOM of the page while doing some operation.

There could many possible reasons for this exception to occur

  • The page is no longer the same or the page has been refreshed, since the element was located
  • The element may have been removed and re-added to the screen like relocation, since it was located
  • Element may have been inside a context which was refreshed

ElementNotVisibleException

selenium.common.exceptions.ElementNotVisibleException

Base: selenium.common.exceptions.InvalidElementStateException

This exception can be imported as ‘from selenium.common.exceptions import ElementNotVisibleException’

This particular exception is raised or thrown when an element is present on the DOM, but it is not visible.

It occurs most commonly when trying to click or read text of an element that is hidden from view.

ElementNotInteractableException

selenium.common.exceptions.ElementNotInteractableException

Base: selenium.common.exceptions.InvalidElementStateException

This exception can be imported as ‘from selenium.common.exceptions import ElementNotInteractableException’

This exception is raised or thrown when an element is present in the DOM but interactions with that element will hit another element

TimeoutException

selenium.common.exceptions.TimeoutException

Base: selenium.common.exceptions.WebDriverException

This exception can be imported as ‘from selenium.common.exceptions import TimeoutException’

This exception is raised or thrown when a command does not complete with in time. Like with in implicit or explicit wait time specified.

For more exception details – Visit Here

Please provide your opinion on comments.

 

Leave a Reply

Your email address will not be published. Required fields are marked *