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', '');
document.getElementsByName('lname')[0].removeAttribute('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).

@Test
  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);
  
  Thread.sleep(2000);
  
  //To disable First Name text box
  JavascriptExecutor javascript = (JavascriptExecutor) driver;
  String todisable = "document.getElementsByName('fname')[0].setAttribute('disabled', '');";
  javascript.executeScript(todisable);
  Thread.sleep(2000);
  
  //To enable Last Name text box
  String toenable = "document.getElementsByName('lname')[0].removeAttribute('disabled');";
  javascript.executeScript(toenable);
  Thread.sleep(3000);
  
  
  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);
    
  }

6 thoughts on “How To Enable/Disable Textbox In Selenium WebDriver On The Fly

  1. Worked like a charm for me in PowerShell:

    $PasswordInput = FindElement ([By]::XPath('//input[@name="passwordinput"]'))
    $Browser.ExecuteScript('arguments[0].removeAttribute("disabled");',$PasswordInput)
    $PasswordInput | SendKeys "$($AccountData.P1)" -clear

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

Leave a Reply

Your email address will not be published. Required fields are marked *