Difference between Selenium visibility methods

There are several methods that Selenium provides, to help you during your test execution to confirm that a specific object exists. This article provides details about and differences between them.

Boolean isSelected() 

This method determines if an element is selected or not. It returns true if the element is selected and false if it is not.  It is widely used on check boxes, radio buttons and options in a select.

Copy

isSelected() syntax

_driver.findElement(By.xpath("XPath Locator")).isSelected( );

The above statement when used with Radio button will return true if the Radio button is selected else it will return false. The above statement when used with Check box option will return true if the Check box option  is selected else it will return false. So we have to use assertTrue( ) predefined method of JUnit to take the above result and:

  • Pass the Test when the result is true
  • Fail the Test when the result is false

So finally the statement to verify whether the radio button is selected will look like below:

Copy
assertTrue("Radio button is not selected", _driver.findElement(By.xpath("XPathForLocatingRadioButton")).isSelected( );

And the statement to verify whether the check box is selected will look like below:

Copy
assertTrue("Check Box is not selected", _driver.findElement(By.xpath("XPathForLocatingCheckBox")).isSelected( );

Boolean isEnabled()

This method determines if an element is enabled or not. It returns true if element is enabled (All elements apart from disabled input elements) and false if otherwise.

isEnabled() checks for the disabled attribute on the button element. If the attribute "disabled" is not present, it returns True, so if you never add this attribute to disabled buttons and instead add the value "disabled" to the button's class, isEnabled() will always return true. 

Copy

Example of a disabled button

<button type="button" disabled>Click Here!</button>

If you need to determining whether the button is enabled or disabled based on a class, you will need to instead check for the existence of a button with the "disabled" class (find it by class name, xpath, or CSS selector) to decide what state the button is in. 

Copy

isEnabled() syntax

boolean file_name = driver.findElement(By.xpath("//input[@name='the_name']")).isEnabled();
System.out.print(file_name);

We can use isEnabled() method with if condition to take action based on element's enabled status on page of software web application. 

isElementPresent()

This method basically tests if the element we are looking for is present somewhere on the page. Observe that isElementPresent() won't mind even if our element is not visible.

Copy

Code samples

#1
isElementPresent() // positive test case
if(isElementPresent(By.cssSelector(your_Value)))
System.out.println("The element present");
else
System.out.println("this element is missing")

#2 Checks the availability using size()
if(driver.findElements(By.id("Value)).size()>0)

{
   System.out.println("The element present");
}else 
{
   System.out.println("this element is missing");
}
#3
!isElementPresent() // negative test caseif(!isElementPresent(By.xpath("Value")))
{
   System.out.println("The element present");
} else
{
   System.out.println("this element is missing");
}

Boolean isDisplayed()

This method determines  if an element is displayed on the screen or not  i.e. whether its width and height are greater than zero, it isn't hidden by CSS, etc. If the element is present on the page, but has style="display:none;" then isDisplayed() will return false.. It returns true if the element is displayed and false if it is not. Advantage of this method is that it avoids parsing an elements style attribute. isDisplayed is used in cases where element is present in DOM and you need to check whether it is displayed or not in the UI. It is never used to check whether an element is present in the DOM.

Copy

isDisplayed()

if(driver.findElement(By.xpath(xpath_itself)).isDisplayed() )                                                                                                         
{         
  /**Do this*/     
}    
else    
{     
  /**Do this*/    
}

isVisible() 

 isVisible is method of old Selenium RC and isDisplayed is method of Selenium 2.

An element can be rendered invisible by setting the CSS "visibility" property to "hidden", or the "display" property to "none", either for the element itself or one if it's ancestors. This method will fail if the element is not present.

isDisplayed() vs isVisible()

WebDriversWebElement contains onlyisDisplayed() method, which by the doc:

isDisplayed()

Is this element displayed or not? This method avoids the problem of having to parse an element's "style" attribute.

Before Webdriver there was Selenium RC, which is now long deprecated, the DefaultSelenium class had isVisible() method that:

isVisible()

Determines if the specified element is visible. An element can be rendered invisible by setting the CSS "visibility" property to "hidden", or the "display" property to "none", either for the element itself or one if its ancestors. This method will fail if the element is not present. It looks for display: none style tag - this might throw a null pointer if we aren't careful

Note:

To see if an element is visible first check if the element is present using isElementPresent() method. Then try checking if the element is visible.

The visibility of an element is guided by what is perceptually visible to the human eye. In this context, an element’s displayedness does not relate to the visibility or display style properties.

The approach recommended to implementors to ascertain an element’s visibility was originally developed by the Selenium project, and is based on crude approximations about an element's nature and relationship in the tree. An element is in general to be considered visible if any part of it is drawn on the canvas within the boundaries of the viewport.