isDisplayed, isSelected, isEnabled in Selenium

HOME

This tutorial describes how to check the state of a WebElement.

In this tutorial, we will learn isDisplayed, isEnabled, isSelected method in Selenium, and how to check the state of a WebElement. There are many methods which are used to determine the visibility scope for the web elements – isSelected(), isEnabled() and isDispalyed().

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 exist in DOM, but it does not exist on the web page.

WebDriver facilitates the user with the following methods to check the visibility of the web elements. These web elements can be buttons, drop boxes, checkboxes, radio buttons, labels etc.

  • isDisplayed()
  • isSelected()
  • isEnabled()
  1. 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.

2. Boolean isDisplayed(): This method determines if an element is displayed or not. 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.

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

Steps to follow to understand when an element isEnabled and isDisplayed.

  1. Launch the web browser and open the application under test – https://duckduckgo.com/
  2. Verify the web page title
  3. Verify if the “Search Box” is displayed
  4. Verify that the “Search Box” is enabled
  5. If Search Box is enabled, then search for text – Selenium
  6. Close the browser
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class VerifyConditionsDemo {

	public static void main(String[] args) {

		System.setProperty("webdriver.gecko.driver",
				"C:\\Users\\Vibha\\Software\\geckodriver-v0.26.0-win64\\geckodriver.exe");
		
WebDriver driver = new FirefoxDriver();

		// launch the firefox browser and open the application url
		driver.get("https://duckduckgo.com/");

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

		// declare and initialize the variable to store the expected title of the
		// webpage.
		String expectedTitle = "DuckDuckGo — Privacy, simplified.";

		// fetch the title of the web page and save it into a string variable
		String actualTitle = driver.getTitle();

		// compare the expected title of the page with the actual title of the page and
		// print the result
		if (expectedTitle.equals(actualTitle)) {
			System.out.println("Verification Pass- The correct title is displayed on the web page.");
		} else {
			System.out.println("Verification Failed - An incorrect title is displayed on the web page.");
		}

		// verify that the “Search" Box is displayed
		WebElement searchBox = driver.findElement(By.id("search_form_input_homepage"));
		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());
		}

		// verify that the “Search” Box is enabled
		if (searchBox.isEnabled()) {
			System.out.println("Search Box is enabled. Return: " + searchBox.isEnabled());
			searchBox.sendKeys("Selenium");
		} else {
			System.out.println("Search Box is not enabled. Return: " + searchBox.isEnabled());
		}

		System.out.println("Successful Execution of Test.");

		// close the web browser
		driver.close();

	}
}

Output
Verification Pass- The correct title is displayed on the web page.
Search Box is visible. Return: true
Search Box is enabled. Return: true
Successfull Execution of Test.

isSelected

Steps to follow to understand when an element is Selected or not.

  1. Launch the web browser and open the application under test – https://www.seleniumeasy.com/test/basic-radiobutton-demo.html
  2. Verify if first Radio Button (Male) is selected or not
  3. If first radio button is already selected, then select Radio Button 2, else vice versa.
  4. Print the value of selected Radio Button
  5. Print the value of selecting a Radio Button
  6. Close the browser

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class isSelectedDemo {

	public static void main(String[] args) {
		System.setProperty("webdriver.gecko.driver",
				"C:\\Users\\Vibha\\Software\\geckodriver-v0.26.0-win64\\geckodriver.exe");

		FirefoxDriver driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
		driver.get("https://www.seleniumeasy.com/test/basic-radiobutton-demo.html");

		List<WebElement> Radio_Options = driver.findElements(By.name("optradio"));

		// Create a boolean variable which will hold the value (True/False)
		boolean radio_value = false;

		// This statement will return True, in case of first Radio button is selected
		radio_value = Radio_Options.get(0).isSelected();

		// Identify if Radio Button 1 is selected or not. If Button 1 is already
		// selected, then select Button 2
		if (radio_value == true) {
			Radio_Options.get(1).click();
			System.out.println("Button Selected is :" + Radio_Options.get(1).getAttribute("value"));
		} else {
			Radio_Options.get(0).click();
			System.out.println("Button Selected is :" + Radio_Options.get(0).getAttribute("value"));
		}

		driver.findElement(By.id("buttoncheck")).click();
		String Button_Selected = driver.findElement(By.xpath("//*[@id='easycont']/div/div[2]/div[1]/div[2]/p[3]"))
				.getText();
		System.out.println("Get Checked Value is :" + Button_Selected);

		// close the web browser
		driver.close();
	}
}

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s