Home » Software Testing » Automation Testing » Selenium WebDriver Tutorial » Check if an element exists using WebDriver

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

 

8 thoughts on “Check if an element exists using WebDriver”

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

  2. Pingback: WebDriver’s most popular Commands | Abode QA

  3. Pingback: Handling Multiple Windows in WebDriver | Abode QA

  4. Pingback: Email your failing Selenium WebDriver scripts’ Stack Trace to your Email Id | Abode QA

  5. Pingback: Unexpected error launching Internet Explorer. Protected Mode must be set to the same value | Abode QA

  6. 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.

%d bloggers like this: