Verify element is enabled in Selenium WebDriver

In this How to verify element is enabled in Selenium WebDriver post you will learn how we can verify particular elements is enabled,During selenium web driver testing or test cases creation,most of the time you need to check targeted elements,whether particular elements are enable or not on web application before doing any actions on page.


Verify element is enabled in Selenium WebDriver

Verify element is enabled in Selenium WebDriver


In order to overcome this problem Selenium WebDriver providing in built method isEnabled() to verify the elements disable or enable status of web applications.Suppose while working on my new web applications we have to verify while creating new customers but here first all the fields are disabled when you enter mobile number and click on Search button then only remaining elements will enable to perform action/create new customers.Elements disable means before performing any action need to enable elements to perform new actions as above example.

Example


isEnabled() - is a Selenium WebDriver method which is developed in Web Element class in order to verify if particular element is enabled or not,in case it is not enabled it will return false or Fail.

Syntax


WebElement Save = driver.findElement(By.id("save_btn"));
boolean save_button=Save.isEnabled();

You can use isEnabled() Selenium WebDriver method in IF condition to check the condition to return ture in case it is verifying the condition of element is enabled or not.We will see isEnabled method with example and below example will give you brief details about how to use isEnabled() method in a method to check element id enabled or not.

As I am using TestNG framework to create the test cases in Selenium WebDriver ,Please follow below steps and code to verify element is enabled or not.

1.Create Class
2.Add TestNG library
3.Create Method with any name
4.Save elements in WebElements
5.Use if condition to verify Whether element is enabled or not.


@Test(priority=1)
public void verify_CustElements() throws InterruptedException{
 
//Store all customer elements in WebElments
WebElement FirstName = driver.findElement(By.id("first_name"));
WebElement Email = driver.findElement(By.id("email"));
WebElement Save = driver.findElement(By.id("save_btn"));
 
//Verify Mobile No field is in disable mode
if(MobileNo.isEnabled()){
Thread.sleep(2500); 
System.out.println("Mobile No field is in Enabled mode"); 
}else{
System.out.println("Mobile No field is in disabled mode,Test Case is FAIL");
}
 
//Verify SAVE button is in disable mode
if(Save.isEnabled()){
 
System.out.println("Save button is in enable mode ,Test Case is PASS");
}else{
 
System.out.println("Save button is in disable mode ,Test Case is FAIL");

}
if(Email.isEnabled()){
System.out.println("Email field is in enabled mode ,Test Case is PASS");
}else{
System.out.println("Email field is in disabled mode ,Test Case is FAIL");
}


As per above example,it will check the status of First name and save button text box and print the message in Eclipse console based on element is enabled or disabled.In case you like, it is helpful ,please share it and please provide valuable comments on this topic and please provide do you have any questions.

Post a comment

0 Comments