Using JavaScriptExecutor to enable input text field value.

1,363 views
Skip to first unread message

new...@gmail.com

unread,
Aug 12, 2013, 6:42:57 AM8/12/13
to webd...@googlegroups.com
Hi,

I have a HTML page which is populated with different values. I need to assert these values and thus get the text value of these fields.
However all the fields are disabled, so basically 

WebElement Ref= driver.findElement(By.id("ref"));
Ref.getText(); 
does not return any value


HTML :
<input id="ref" class="form_fields_readonly" type="text" readonly="" value="" name="ref" alt="" disabled="">

I need help with using Javascript executor to enable the field, so that webdriver can recognize and get the value of this field.

Thanks,



David

unread,
Aug 14, 2013, 9:11:30 PM8/14/13
to webd...@googlegroups.com
Since you are extracting value, and will have to use JavascriptExecutor anyways, then it is far simpler to just use JavascriptExecutor to extract the value in 1 step than to enable with javascript then extract value with WebDriver API in 2 steps. Here's how you could do it:

String theTextIWant = ((JavascriptExecutor) driver).executeScript("return arguments[0].value;",driver.findElement(By.id("ref")));

You just use javascript to extract out the value attribute of the input field. If it were a div or textarea, then you'd like use the innerHTML, innerText, or textContent attributes (the latter may be best).

If you just did want to enable only, it'd probably be something like this, assuming you just need to set some fields to false. If you have to remove (readlonly and disabled) attributes that's a little different. Not sure how to do that, you can search online for the javascript for that and follow example below to figure out how to do that.

//you might also need to change class value to something like "form_fields" without the "_readonly" assuming such a non-read-only class exists
((JavascriptExecutor) driver).executeScript("arguments[0].readonly = false; arguments[0].disabled = false;",driver.findElement(By.id("ref")));
Reply all
Reply to author
Forward
0 new messages