SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

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

isEnabled()
isEnabled() webdriver method will verify and return true if specified element is enabled. Else it will return false. 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 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.

1 comment:

  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