Check if an element exists using 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 element is on page or not and than 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 specific action. There might be possibility like element has not be loaded in to DOM and due to this exception is coming and stating something related to this “Element not found”
[the_ad_placement id=”incontent”]

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


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

[the_ad_placement id=”incontent”]
Case 4:
Why not we use isDisplayed() method first in If condition like this

Case 5:

Before using any of the element in code, try to find whether locator has been correctly written and check 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!!!!



Leave a Reply

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