Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Validate The Validation You Do – isEnabled() & isSelected() Methods in Selenium WebDriver – Make Selenium Easy

Posted on 05/19/2022 By admin

Front end Automation testing is not only about Navigating/Clicking/Typing here and there. It is more about validation so that it could catch real bugs. 

If we use Selenium WebDriver for front end automation, it provides methods to do some validation like isEnabled(), isDisplayed() , isSelected() etc. We must need to understand the correct usage of methods before using it and if it is serving actual purpose.

I want to give an example here of how people use wrong method to validate something. Let’s consider below scenario:

  1. Navigate to “http://makeseleniumeasy.com“.
  2. Verify “Home” tab is active.
  3. Click on “API Testing”.
  4. Verify “Home” tab is not active but “API Testing” is active now.

Above scenario is similar to a real time scenario whose automation script I saw and got an idea of writing this post. You may be thinking its a very simple scenario. We just need to call isEnabled() method to get if a particular tab is active or inactive. Is it a correct validation? Actually NO.

“isEnaled()” method does not work in above way. Selenium javadoc defines it as below:

” Is the element currently enabled or not? This will generally return true for everything but disabled input elements. “. This method checks for “disable” attribute in web elements like input elements, buttons, textareas, Select etc. A link (An anchor tag ‘a’) can not have “disable” property. So calling isEnabled() method to check if a link in enabled or disabled is not correct.

Reference:

https://github.com/SeleniumHQ/selenium/issues/1377

https://github.com/SeleniumHQ/selenium/issues/1348

Now you can think of using isSelected() method. Will take do the correct validation of above scenario? Let’s see.

Selenium javadoc defines isSelected() method as below:

“Determine whether or not this element is selected or not. This operation only applies to input elements such as checkboxes, options in a select and radio buttons. For more information on which elements this method supports, refer to the specification. “

You should understand now that you can not use isEnabled() or isSelected() method to check if a link is active or inactive or selected.

How to do above validation then?

We need to observe the difference in DOM when tab is active or inactive or selected. For example:- See below images:-

when Home tab is selected, its class attribute contains a value named ‘current-menu-item’.

When I selected other tab and Home tab is no more selected, its class attribute does not contain ‘current-menu-item’.

Now we can drive the logic here. We just need to get class attribute and verify if it contains ‘current-menu-item’ if it is Active. In case of inactive, it should not be available.

We will take another example of different site here:-


Selenium Code:-

package Problems; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class VerifyTabStatus { public static void main(String[] args) throws InterruptedException { System.setProperty(“webdriver.chrome.driver”, “./exefiles/chromedriver.exe”); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get(“http://automationpractice.com/”); // Locating Women tab WebElement tabWomen = driver.findElement(By.xpath(“//a[@title=’Women’]/..”)); // CLicking on Women tab tabWomen.click(); // Relocating otherwise it will give StaleElementException tabWomen = driver.findElement(By.xpath(“//a[@title=’Women’]/..”)); // If we use isSelected() or isEnabled() method on it System.out.println(“===========Verifying using isEnabled and isSelected method===========”); System.out.println(“Women Tab status as per isEnabled() method when it is selected: ” + tabWomen.isEnabled()); System.out.println(“Women Tab status as per isSelected() method when it is selected:: ” + tabWomen.isSelected()); // Click on another tab Dresses driver.findElement(By.xpath(“(//a[@title=’Dresses’]/..)[2]”)).click(); Thread.sleep(15000); // Relocating Home tab tabWomen = driver.findElement(By.xpath(“//a[@title=’Women’]/..”)); // If we use isSelected() or isEnabled() method on it again. You will see there is no change. So it is not correct way of validation. System.out.println(“Home Tab status as per isEnabled() method when it is not selected: ” + tabWomen.isEnabled()); System.out.println(“Home Tab status as per isSelected() method when it is not selected:: ” + tabWomen.isSelected()); // Above code does not validate what actually we want to do System.out.println(“==============Verify using attributes=======================================”); driver.get(“http://automationpractice.com/”); // Locating Women tab tabWomen = driver.findElement(By.xpath(“//a[@title=’Women’]/..”)); // CLicking on Women tab tabWomen.click(); // Relocating otherwise it will give StaleElementException tabWomen = driver.findElement(By.xpath(“//a[@title=’Women’]/..”)); // Retrieving value of class attribute String classValue = tabWomen.getAttribute(“class”); if (classValue.contains(“sfHoverForce”)) System.out.println(“Women tab is currently selected.”); // Click on another tab Dresses driver.findElement(By.xpath(“(//a[@title=’Dresses’]/..)[2]”)).click(); Thread.sleep(15000); // Relocating Home tab tabWomen = driver.findElement(By.xpath(“//a[@title=’Women’]/..”)); // Retrieving value of class attribute String classValueAfter = tabWomen.getAttribute(“class”); if (!(classValueAfter.contains(“sfHoverForce”))) System.out.println(“Women tab is currently not selected.”); driver.quit(); } }

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class VerifyTabStatus { public static void main(String[] args) throws InterruptedException { System.setProperty(“webdriver.chrome.driver”, “./exefiles/chromedriver.exe”); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get(“http://automationpractice.com/”); WebElement tabWomen = driver.findElement(By.xpath(“//a[@title=’Women’]/..”)); // Relocating otherwise it will give StaleElementException tabWomen = driver.findElement(By.xpath(“//a[@title=’Women’]/..”)); // If we use isSelected() or isEnabled() method on it System.out.println(“===========Verifying using isEnabled and isSelected method===========”); System.out.println(“Women Tab status as per isEnabled() method when it is selected: ” + tabWomen.isEnabled()); System.out.println(“Women Tab status as per isSelected() method when it is selected:: ” + tabWomen.isSelected()); // Click on another tab Dresses driver.findElement(By.xpath(“(//a[@title=’Dresses’]/..)[2]”)).click(); tabWomen = driver.findElement(By.xpath(“//a[@title=’Women’]/..”)); // If we use isSelected() or isEnabled() method on it again. You will see there is no change. So it is not correct way of validation. System.out.println(“Home Tab status as per isEnabled() method when it is not selected: ” + tabWomen.isEnabled()); System.out.println(“Home Tab status as per isSelected() method when it is not selected:: ” + tabWomen.isSelected()); // Above code does not validate what actually we want to do System.out.println(“==============Verify using attributes=======================================”); driver.get(“http://automationpractice.com/”); tabWomen = driver.findElement(By.xpath(“//a[@title=’Women’]/..”)); // Relocating otherwise it will give StaleElementException tabWomen = driver.findElement(By.xpath(“//a[@title=’Women’]/..”)); // Retrieving value of class attribute String classValue = tabWomen.getAttribute(“class”); if (classValue.contains(“sfHoverForce”)) System.out.println(“Women tab is currently selected.”); // Click on another tab Dresses driver.findElement(By.xpath(“(//a[@title=’Dresses’]/..)[2]”)).click(); tabWomen = driver.findElement(By.xpath(“//a[@title=’Women’]/..”)); // Retrieving value of class attribute String classValueAfter = tabWomen.getAttribute(“class”); if (!(classValueAfter.contains(“sfHoverForce”))) System.out.println(“Women tab is currently not selected.”);

===========Verifying using isEnabled and isSelected method=========== Women Tab status as per isEnabled() method when it is selected: true Women Tab status as per isSelected() method when it is selected:: false Home Tab status as per isEnabled() method when it is not selected: true Home Tab status as per isSelected() method when it is not selected:: false ==========Verify using attributes======================================= Women tab is currently selected. Women tab is currently not selected.

===========Verifying using isEnabled and isSelected method=========== Women Tab status as per isEnabled() method when it is selected: true Women Tab status as per isSelected() method when it is selected:: false Home Tab status as per isEnabled() method when it is not selected: true Home Tab status as per isSelected() method when it is not selected:: false ==========Verify using attributes======================================= Women tab is currently selected. Women tab is currently not selected.

Hope you learnt a new thing and will be careful when using these types of scenario. And also hope that you know where to use isEnabled() and isSelected() methods.

In case of any query, doubt kindly comment. If you like my posts, please share to reach maximum.

#ThanksForReading

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Uncategorized

Post navigation

Previous Post: Hierarchy of Classes & Interfaces of WebDriver Interface in Selenium WebDriver – Make Selenium Easy
Next Post: Postman Tutorial Part 41 – Dynamic Variables in Postman – Make Selenium Easy

Related Posts

TestNG Tutorials 54: DataProvider in TestNG – Implementing Object Oriented Concept “Encapsulation” with DataProvider Method | Make Selenium Easy Uncategorized
September 7, 2017 – Make Selenium Easy Uncategorized
RegenerateTestNG – Make Selenium Easy Uncategorized
Frequently Asked Java Program 03: Java Program to check if any string is palindrome Using inbuilt Reverse method of Java Uncategorized
Common Mistakes & Best Practices in Selenium Uncategorized
image – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark