How To Enable/Disable Textbox In Selenium WebDriver On The Fly

We can know element's enabled/disabled status very easily using isEnabled() method in selenium webdriver software test as described in THIS EXAMPLE POST. Now supposing you have a scenario where you wants to enable any disabled text box or disable any enabled text box during software test execution then how to do it? Webdriver do not have any built in method by which you perform this action. But yes, Webdriver has JavascriptExecutor interface by which we can execute any javascript using executeScript() method.

I have posted many posts on how to execute javascript in selenium webdriver software testing tool to perform different actions. You can view all those example on THIS LINK. Same way, we can enable or disable any element using JavascriptExecutor interface during webdriver software test case execution. To disable text box, we will use html dom setAttribute() method and to enable text box, we will use html dom removeAttribute() method with executeScript() method. Javascript syntax for both of these is as bellow.

document.getElementsByName('fname')[0].setAttribute('disabled', '');

Now let we use both these javascripts practically to understand them better. VIEW THIS POST to see how to enable or disable text box in selenium IDE software testing tool.

Copy bellow given @Test method part of enable or disable element 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).

  public void test () throws BiffException, IOException, InterruptedException 
  boolean fbefore = driver.findElement(By.xpath("//input[@name='fname']")).isEnabled();
  System.out.print("\nBefore : First Name Text box enabled status is : "+fbefore);
  boolean lbefore = driver.findElement(By.xpath("//input[@name='lname']")).isEnabled();
  System.out.print("\nBefore : Last Name Text box enabled status is : "+lbefore);
  //To disable First Name text box
  JavascriptExecutor javascript = (JavascriptExecutor) driver;
  String todisable = "document.getElementsByName('fname')[0].setAttribute('disabled', '');";
  //To enable Last Name text box
  String toenable = "document.getElementsByName('lname')[0].removeAttribute('disabled');";
  boolean fafter = driver.findElement(By.xpath("//input[@name='fname']")).isEnabled();
  System.out.print("\nAfter : First Name Text box enabled status is : "+fafter);
  boolean lafter = driver.findElement(By.xpath("//input[@name='lname']")).isEnabled();
  System.out.print("\nAfter : Last Name Text box enabled status is : "+lafter);


  1. what is the meaning of ('fname')[0] is it stored inside an array....or when to use [0]. please explain

    1. getElementsByName will return the list of elements and from them we have to find first element. that's why used [0].

  2. Hi, can i get javasript methods and properties in eclipse? Please Reply..

    1. @Archana you may import the below package then you can create an object and then use it
      import org.openqa.selenium.JavascriptExecutor;

      Once above statement is included then

      JavascriptExecutor js = (JavascriptExecutor)driver;
      js.executeScript("return document.domain");

      Like this you can run the script

  3. Worked like a charm for me in PowerShell:

    $PasswordInput = FindElement ([By]::XPath('//input[@name="passwordinput"]'))
    $PasswordInput | SendKeys "$($AccountData.P1)" -clear

  4. Thanks was able to implement this successfully smoothly