Difference Among isDisplayed(), presenceOfXXX() and visibilityOfXXX() Methods In Selenium Webdriver

Hello Folks,

We generally used some methods for Eg. isDisplayed(), presenceOfXXXX(), visibilityOfXXXX() etc while developing test scripts.  All methods look similar but do you know there are some differences among them. We will learn about those differences in this post.

isDispalyed():

  • This method checks whether or not the element is displayed in visible area of browser.
  • It returns TURE if displayed in browser’s visible are otherwise false.
  • This method avoids the problem of having to parse an element’s “style” attribute.

Let’s understand with an example:

Steps to do:

  • Launch Flipkart URL.
  • Mouse hover on “Home & Furniture” menu so that sub menus will be displayed.
  • In sub menu, you will see an option “Bedsheets”.
  • Just locate “Bed sheets” as shown below:

  • Call isDisplayed() method on Bedsheets element. You will get TRUE.
  • Now, copy the locator of Bedsheets element and remove cursor from “Home & Furniture” so that Bedsheets option will not be displayed on screen.
  • Now, just paste locator and hit enter. Shocked!!

  • Element “Bedsheets” is not displayed on browser but you are able to locate Bedsheets web element. If you call isDisplayed on Bedsheets web element now, you will get false.
  • Are you able to understand what is behavior of isDisplayed method now?

Java Code:

Output: Element is displayed.

Element is not displayed.