Friday, 14 February 2014

How to verify button is disabled in Selenium Web driver in Java?


Selenium web driver provides one method called - isEnabled which can be used to check if the button is enabled or disabled in Selenium Webdriver in Java.

boolean actualValue = e.isEnabled();
above code will check if button e is enabled or disabled. If it is enabled, actualValue will be true. If it is disabled actualValue will be false.

Full example in Java with selenium webdriver to check if button is disabled

package temp;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class first {

public static void main(String[] args) {
              // TODO Auto-generated method stub

System.setProperty("webdriver.chrome.driver""C:\\Selenuim\\chromedriver2.3.exe");
WebDriver driver =  new ChromeDriver();

try{
driver.get("http://register.rediff.com/register/register.php");

Thread.sleep(2000);
WebElement e = driver.findElement(By.name("btnemail"));

boolean actualValue = e.isEnabled();

if (actualValue)
       System.out.println("Button is enabled");
else
       System.out.println("Button is disabled");
      
Thread.sleep(2000);

}

catch(Exception ex){
       System.out.println("Exception " + ex.getMessage());
              }
              finally{
                    
                     driver.close();
                     driver.quit();
              }
       }

}


Please note that we can check the disable property of all kinds of elements like button, checkbox, radiobutton, checkbox, combobox in similar way using isEnabled method in Java using selenium webdriver.


What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

3 comments:

  1. The isEnabled function always delivers true, for all elements but the input element

    https://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#isEnabled()

    ReplyDelete
  2. @Fred: the javadoc threw me off at first, but it seems input elements in general are meant, not just < input >-elements. Just tested isEnabled() with a select element (dropdown) and it works just fine (returns false when dropdown is disabled).

    ReplyDelete

Buy Best Selenium Books

Contributors