The differences between NoSuchElementException and ElementNotFoundException

I am bit confused here,I encounter NotFoundException , Is it different from above two?

1 Like

For Selenium Context, There is no ElementNotFoundException

The NotFoundException is a Super Class which includes the Sub Class NoSuchElementException.

To understand it more, we can say

NoSuchElementException extends NotFoundException

and

NotFoundException extends WebDriverException“

Your script will throw NoSuchElementException when

1. Element is not present in DOM – or may it never exists------------------>In this case you need to modify your findElement locator search

2. The page is still loading and you’ve already finished your WebElement search / AJAX Element has not returned yet on the page ------------------->The fix for this is to use WebDriverWait to wait for the element to appear (visibility and/or clickable)

3. Sometimes, wrong approach/missing steps in locating an element will also throw NoSuchElementException. For example, the user selects Country and javascript populates a City field. If you attempt to look for a city before you select a country, the city you are looking for does not exist --------------->To fix this you have to make sure the steps in your test are correct.

The other known subclasses of NotFoundException are:

–>NoAlertPresentException

–>NoSuchContextException

–>NoSuchFrameException

–>NoSuchWindowException .

We will further discuss these Subclasses as well. For now, I think you would be cleared with confusion :blush: :blush:

Thank you!

5 Likes

Thanks :star_struck: