isDisplayed, isEnabled, isSelected Methods in Selenium

In this tutorial, we will learn isDisplayed, isEnabled, isSelected method in Selenium, and how to check the state of a WebElement.

Many a time a test fails when we click on an element or enter text in a field. This is because the element is displayed or exists in DOM, but it does not exist on the web page.

The result would be that there was an error and the test failed. So, we can handle these kinds of problems by checking the visibility of web elements on webpage. We can easily know the state of an element.

isEnabled() Method in Selenium


isEnabled() method is used to check if the web element is enabled or disabled within the web page. This method returns “true” value if the specified web element is enabled on the web page otherwise returns “false” value if the web element is disabled on the web page.

The syntax for isEnabled() method in Selenium WebDriver is as follows:

boolean isEnabled()

The above syntax can be implemented in coding like this:

driver.findElement(By.locatorType("path")).isEnabled();

Let’s take an example to automate Google home page as an application under test that will check the status of element using isEnabled() method.

In general, user is not allowed to enter the text in search box when the search box is disabled. If it is enabled status, it should return true in the case.


Scenario to be automated:

1. Launch the web browser and open the application under test – https://www.google.com
2. Verify the web page title.
3. Verify that “Google Search Box” is enabled or not.
4. Enter the text in the “Google Search” box.
5. Verify that the “Google Search button” is enabled or not.

Let’s write the code to automate the above scenario.

Program code 1:

package selenium; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class IsEnabledMethodEx { 
public static void main(String[] args) 
{ 
 // Create an object of FirefoxDriver class. 
    WebDriver driver = new FirefoxDriver(); 

 // Launch the Firefox browser and open URL. 
    String URL = “https://www.google.com”; 
    driver.get(URL); 

 // Maximize browser window. 
    driver.manage().window().maximize(); 

 // Delay for specified amount of time to load page. 
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS); 

 // Declare and initialize variable to store the expected title of the webpage. 
    String expectedTitle = “Google”; 

 // Fetch title of the web page and save it using a variable actualTitle with data type string. 
    String actualTitle = driver.getTitle(); 

 // Compare expected title of the page with actual title of the page and print the result. 
    if(expectedTitle.equals(actualTitle)) 
    { 
       System.out.println(“Verification Successful – Correct title is displayed on the home webpage.”); 
    } 
    else 
    { 
       System.out.println(“Verification Failed: Incorrect title is displayed on the home webpage.”); 
    } 
 // Check that “Google Search box” is enabled or not. If Search box is enabled, it will return true. 
    WebElement searchBox = driver.findElement(By.xpath(“//input[@name=’q’]”)); 
   
    if(searchBox.isEnabled()) 
    { 
       System.out.println(“Search box is enabled. Return: ” +searchBox.isEnabled()); 
    } 
    else 
    { 
       System.out.println(“Search box is disabled. Return: ” +searchBox.isEnabled()); 
    } 
 // Enter text in the “Google Search” box. 
    WebElement sendText = driver.findElement(By.xpath(“//input[@name=’q’]”)); 
    sendText.sendKeys(“Selenium Tool”); 

 // Check that “Google Search button” is enabled or not. 
    WebElement searchButton = driver.findElement(By.xpath(“//input[@class=’gNO89b’]”)); 
    if(searchButton.isEnabled()) { 
         System.out.println(“Search button is enabled. Return: ” +searchButton.isEnabled()); 
    } 
    else { 
         System.out.println(“Search button is not enabled. Return: ” +searchButton.isEnabled()); 
    } 
    driver.close(); // Closing the driver.
 } 
}
Output: 
       Verification Successful – Correct title is displayed on the home webpage. 
       Search box is enabled. Return: true 
       Search button is enabled. Return: true

In the preceding example program, we have used isEnabled() method of WebElement class to check that search box and search button is enabled or not. Both elements are enabled because they returned true.

isDisplayed() Method in Selenium


The isDisplayed() method is used to check whether an element is displayed on a web page or not. It returns a boolean value (true) if the target element is displayed otherwise returns false. The syntax for isDisplayed() method in Selenium is as follows:

boolean isDisplayed()

The syntax can be implemented in coding like this:

driver.findElement(By.locatorType("path")).isDisplayed();

The preceding method confirms whether target element is visible on a web page until the timeout occurs. In the above scenario, we have checked that the search button is enabled on the web page.

Now we will check further that search button is displayed (visible) or not on the web page. If it is displayed, it will return true otherwise false. Look at the following source code given below.

Program code 2:

package selenium; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class IsDisplayedMethodEx { 
public static void main(String[] args) 
{ 
 // Create an object of FirefoxDriver class. 
    WebDriver driver = new FirefoxDriver(); 

 // Launch the Firefox browser and open URL. 
    String URL = "https://www.google.com"; driver.get(URL); 

 // Maximize browser window. 
    driver.manage().window().maximize(); 

 // Delay for specified amount of time to load page. 
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS); 

 // Check that “Google Search box” is displayed or not. If Search box is displayed, it will return true. 
    WebElement searchBox = driver.findElement(By.xpath("//input[@name='q']")); 
     
    if(searchBox.isDisplayed()) { 
        System.out.println("Search box is visible. Return: " +searchBox.isDisplayed()); 
    } 
    else { 
        System.out.println("Search box is not visible. Return: " +searchBox.isDisplayed()); 
    } 
 // Check that "Google Search button" is displayed or not. 
    WebElement searchButton = driver.findElement(By.xpath("//input[@class='gNO89b']")); 
   
    if(searchButton.isDisplayed()) { 
       System.out.println("Search button is visible. Return: " +searchButton.isDisplayed()); 
    } 
   else { 
       System.out.println("Search button is not visible. Return: " +searchButton.isDisplayed()); 
    } 
    driver.close(); 
 } 
}
Output: 
       Search box is visible. Return: true 
       Search button is not visible. Return: false

isSelected() Method in Selenium


The isSelected() method checks that if an element is selected on the web page or not. It returns a boolean value (true) if selected, else false for deselected. It can be executed only on a  radio button, checkbox, and so on. The syntax for isSelected() method in Selenium is as follows:

boolean isSelected()
or,    
driver.findElement(By.locatorType("path")).isSelected();

Let’s take an example to automate “https://selenium08.blogspot.com/2019/07/check-box-and-radio-buttons.html” as an application under test that will check the status of selection of radio button and checkbox using isSelected() method.

Scenario to be automated:

1. Launch web browser and open the application under test –https://selenium08.blogspot.com/2019/07/check-box-and-radio-buttons.html
2. Verify that “Red checkbox” is enabled or not.
3. Check that “Red checkbox” is selected or not.
4. Check that “Opera radio button” is selected or not.

Let’s write the code to automate this scenario:

Program code 3:

package selenium; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class IsSelectedMethodEx { 
public static void main(String[] args) 
{ 
   WebDriver driver = new FirefoxDriver(); 
   String URL = "https://selenium08.blogspot.com/2019/07/check-box-and-radio-buttons.html"; 
   driver.get(URL); 
  
   driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
   WebElement red = driver.findElement(By.xpath("//input[@name='color' and @value='red']")); 

// Check if it is enabled before selecting it. 
   if(red.isEnabled()) { 
       System.out.println("Red checkbox is enabled. Return: " +red.isEnabled()); 

    // Since check box is enabled. Therefore, click it. 
       red.click(); 

    // Now check that it is selected or not. 
       if(red.isSelected()) { 
            System.out.println("Red checkbox is selected. Return: " +red.isSelected()); 
       } 
       else { 
           System.out.println("Red checkbox is not selected. Return: " +red.isSelected()); 
       } 
    } 
    else { 
        System.out.println("Red checkbox is not enabled. Return: " +red.isEnabled()); 
    } 
 // Find XPath for radio button Opera. 
    WebElement opera=driver.findElement(By.xpath("//input[@name='browser' and @value='Opera']")); 

 // Check radio button opera is selected or not. 
    if(opera.isSelected()) { 
        System.out.println("Radio button opera is selected. Return: " +opera.isSelected()); 
    } 
    else { 
        System.out.println("Radio button opera is not selected. Return: " +opera.isSelected()); 
    } 
    driver.close(); 
 } 
}
Output: 
       Red checkbox is enabled. Return: true 
       Red checkbox is selected. Return: true 
       Radio button opera is not selected. Return: false

Key point:

To select a checkbox or radio button, you will have to call click() method. After calling click() method, you can use isSelected() method to see whether it is selected or not.


In this tutorial, we have covered a brief overview of checking the visibility of the state of WebElement in Selenium WebDriver. Hope that you will have understood how to use isDisplayed, isEnabled, and isSelected methods in Selenium and enjoyed it. In the next, we will understand how to select radio button in Selenium WebDriver.
Thanks for reading!!!

⇐ PrevNext ⇒

Please share your love