How To Verify Element Is Enabled Or Disabled in Selenium WebDriver

During your Selenium WebDriver software testing test case creation, frequently you need to verify that your targeted element is enabled or disabled on page of software web application before performing action on it. Webdriver has built in method isEnabled() to check the element enable status on software web application's page. Verification of element is disable and verification of element invisible is totally different for any software web application. Many times you need selenium check if button is disabled, checkbox is enabled or disabled.
Do you know how to check element is disabled in selenium webdriver? Element is disabled means it is visible but not editable and element is invisible means it is hidden. VIEW THIS EXAMPLE to know how to wait till element is visible on the page of software web application.

selenium is Enabled()

isEnabled() selenium webdriver method will verify and return true if specified element is enabled. Else it will return false. Here is sample example for is enabled selenium. Generic syntax to store and print element's status value is as bellow.
boolean fname = driver.findElement(By.xpath("//input[@name='fname']")).isEnabled();
System.out.print(fname);

We can use selenium isEnabled() method with if condition to take action based on element's enabled status on page of software web application. Bellow given example will explain you deeply about isEnabled() webdriver method. If you are using selenium IDE software testing tool then VIEW THIS POST to know how to wait for targeted element becomes editable.

Copy bellow given @Test method part of check element enabled status and replace it with the @Test method part of example given on THIS PAGE(Note : @Test method is marked with pink color in that linked page).
@Test
  public void test () throws BiffException, IOException, InterruptedException 
  {
  boolean fname = driver.findElement(By.xpath("//input[@name='fname']")).isEnabled();
  System.out.print(fname);
  
  WebElement firstname = driver.findElement(By.xpath("//input[@name='fname']"));
  WebElement lastname = driver.findElement(By.xpath("//input[@name='lname']"));
  
  //Verify First name text box is enabled or not and then print related message.
  if(firstname.isEnabled())
  {
   System.out.print("\nText box First name is enabled. Take your action.");
  }
  else
  {
   System.out.print("\nText box First name is disabled. Take your action.");
  }
  
  //Verify Last name text box is enabled or not and then print related message.
  if(lastname.isEnabled())
  {
   System.out.print("\nText box Last name is enabled. Take your action.");
  }
  else
  {
   System.out.print("\nText box Last name is disabled. Take your action.");
  }
  }

Above given example, will check the status of First name and Last name text box and print message in console based on element is enabled or disabled. Same way selenium check if button is disabled or enabled using selenium is enabled method.

3 comments:

  1. boolean bnext = driver.findElement(By.cssSelector("btn.btn-default.btn-next")).isEnabled();
    System.out.println(bnext);

    if (bnext.isEnabled())

    Eclips always show: Cannot invoke isEnabled() on the primitive type boolean

    ReplyDelete
    Replies
    1. It should be

      if(bnext == true)

      // As the return type is Boolean, and it will only send True or False.

      Delete
  2. That's because you defined bnext as a boolean. You can't check .isEnabled() on a boolean. Define bnext as a WebElement and it will work with some other minor changes. Change your code to the below...

    WebElement bnext = driver.findElement(By.cssSelector("btn.btn-default.btn-next"));
    System.out.println(bnext.isEnabled());

    if (bnext.isEnabled())

    ReplyDelete