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!!!!

Dwarika Dhish Mishra

My name is Dwarika Dhish Mishra, its just my name and I am trying to bring the worth of my name in to actions and wants to be the solution not the problem. I believe in spreading knowledge and happiness. More over I am fun loving person and like travelling a lot. By nature I am a tester and a solution maker. I believe in the tag line of http://ted.org “Idea worth spreading” . For the same, I have created this blog to bring more and more learning to tester fraternity through day to day learning in professional and personal life. All contents are the part of my learning and so are available for all..So please spread the contents as much as you can at your end so that it could reach to every needful people in testing fraternity. I am pretty happy that more and more people are showing interest to become the part your Abode QA blog and I think this is good sign for us all because more and more content would be before you to read and to cherish. You may write or call me at my Email id: dwarika1987@gmail.com Cell: 9999978609

You may also like...

9 Responses

  1. autumnator says:

    With regards to isDisplayed() and images, one might want to take a look at this helpful post:

    http://watirmelon.com/2012/11/27/checking-an-image-is-actually-visible-using-webdriver/

  2. atul says:

    With case 2 and case 4 what if i=0 and if the element is not displayed, you are still gonna wait otherwise your script fails!

  3. Roopesh says:

    Excellent Information in u r posts ,i find most solutions from ur blog,Thank u

  4. amit singh says:

    Dear, Dwarika

    I have been a regular visitor of your website and i thank u for your efforts.
    i am new to selenium webdriver and i am continue facing an issue of “modal dialogue issue”.

    Could u help me that how to handle this issue in firefox.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.