Pages

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