Handling Element Not Found Exception In Selenium WebDriver

Handling Element Not Found Exception In Selenium WebDriver

IF you are a Selenium WebDriver  Automation tester than most probably you would have gone through this situation, In which you have faced one exception that speaks “Element not found“.

So why not, we get together and find first whether the element is on-page or not and then we perform the action on it.

Case 1:
Why not use implicit wait or explicit wait before the line of code that finds the element for a specific action. There might be possibility like element has not to be loaded into DOM and due to this exception is coming and stating something related to this “Element not found”

Case 2:
If Case 1: is not able to handle your problem then why not we use this line of code

Int i=driver.findElement(By.xapth("your xpath")).size();
if(i>0)
 {
   System.out.println("Finally we got the element and now do the action on it");
 }

Case 3: If you are using ChromeDriver and trying to click on the certain button than
try the same code on Firefox because Chrome Driver throws an exception that speaks like Element is not present at the location with the co-ordinates and if it working on firefox then get back to code and in place of the simple driver.findelement try javascriptExecutor to click the button and it will work.

Case 4:
Why not we use isDisplayed() method first in If a condition like this

if(dirver.findElement(By.id("Id of Element")).isDisplayed())
{ //now perform the action on this element}

Case 5:

Before using any of the elements in code, try to find whether the locator has been correctly written and check the locator is picking the right set of WebElement from the Web-Page. If you want to learn how to find whether your locator is correct or not, in that case, go and read post

Inspecting Elements for writing XPath, CSS Selector in Chrome

Good Luck!!!!