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.

presenceOfXXXX():

  • You can find many presenceOf methods in ExpectedConditions class.
  • Return type of these methods is  ExpectedCondition<WebElement> i.e. located element it will return if located otherwise org.openqa.selenium.TimeoutException.
  • This method checks if an element is present on the DOM of a page. This does not necessarily mean that the element is visible on browser’s visible area. It may or may not displayed on screen.
  • If you want to perform any action on non-visible element, you will get org.openqa.selenium.ElementNotVisibleException.
  • So, this method if you use, you must use isDisplayed method on located web element to know if element is really displayed in browser’s visible area before performing any action.
  • I have seen many people asking that they are getting org.openqa.selenium.ElementNotVisibleException even after using presenceOf methods.

Java code:

Output:

Exception in thread “main” org.openqa.selenium.ElementNotVisibleException: element not visible

visibilityOfXXXX():

  • You can find many visibilityOf methods in ExpectedConditions class.
  • Return type of these methods is  ExpectedCondition<WebElement> i.e. located element it will return if located otherwise org.openqa.selenium.TimeoutException.
  • This method checks if  an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
  • Now you can see visibiltyOf method is combined form of isDisplayed() and presenceOf() methods. It alone does work of both methods.

I hope you must be able to figure out differences. If you have any doubt, feel free to comment below.

If you like my posts, please like, comment, share and subscribe.

#ThanksForReading

#HappySelenium

 

4 thoughts on “Difference Among isDisplayed(), presenceOfXXX() and visibilityOfXXX() Methods In Selenium Webdriver

Leave a Reply

Your email address will not be published. Required fields are marked *