Kindle Q
Kindle Q

Reputation: 944

How to Disable a TextBox in Selenium

I tried to disabled a textbox, but it doesn't work as expected. The following is my code

the disabling command:

JavascriptExecutor js =    (JavascriptExecutor)driver; 
WebElement textbox = driver.findElement(By.id("textbox"));
js.executeScript("arguments[0].setAttribute('disabled', '');", textbox );

the testing command:

js.executeScript("arguments[0].setAttribute('value', 'Text Box Enalbled !! ');", textbox );

Upvotes: 0

Views: 1616

Answers (4)

user12040159
user12040159

Reputation: 1

JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("document.getElementById('name').value='singh'");

Upvotes: 0

user6757178
user6757178

Reputation:

To disable the textbox in javascript you can do:

JavascriptExecutor js =    (JavascriptExecutor)driver; 
js.executeScript("document.getElementById('textbox').setAttribute('disabled', '');");

Upvotes: 1

Saurabh Gaur
Saurabh Gaur

Reputation: 23805

For disable text box, you need to set .disabled property of the textbox to true as below :-

JavascriptExecutor js = (JavascriptExecutor)driver; 
WebElement textbox = driver.findElement(By.id("textbox"));
js.executeScript("arguments[0].disabled = true", textbox);

Upvotes: 2

Rudziankoŭ
Rudziankoŭ

Reputation: 11251

JavascriptExecutor javascript = (JavascriptExecutor) driver;
String todisable = "document.getElementsById('textbox')[0].setAttribute('disabled', '');";
javascript.executeScript(todisable);

Upvotes: 1

Related Questions