How to getText from the disabled input field in selenium webdriver

1 min


666
1 comment, 666 points
gettext from the disabled input
gettext from the disabled input

How to getText from the disabled input field in selenium webdriver

How to getText from the disabled input field in selenium webdriver it might prove to be a bit tricky if you you are used to get text using the “normal way” locator.text or locator.getText() in java.

You might have a disabled input field that dynamically generates values like the following piece of html:

<input disabled="disabled" id="space_2_total" readonly="readonly" type="text">

Your first instinct will be write [code] locator.text  in ruby or locator.getText()  in java depending of your language and you’ll end up in the situation that your returned result will be null.

The solution for this is to use the html value attribute and you can do it like this :

locator.getAttribute("value")

or in ruby :

locator['value']

This guy basically is going to return you a string and from here you can play with it as long as you want.

The second solution is to get the text using java script executor, something like this :

page.evaluate_script(" $('css_locator').val()")

or in java

return (String) ((JavascriptExecutor) this.webDriver).executeScript(" $('css_locator').val()");

This is just a guide that it might or it might not work for you , but as you know html value attribute specifies the value of an <input> element. The value attribute is used differently for different input types: For “button”, “reset”, and “submit” – it defines the text on the button. For “text”, “password”, and “hidden” – it defines the initial (default) value of the input field.

If you find yourself in one of this situation try this solution out and see where it goes.

If you want to know more about element attribute your can read our article How to use : Capybara get attribute .

I hope this article unblocked you and if you have any suggestions for new topics that you want to find answers please do not hesitate to give us a shot.

Happy Testing!

 

 


Like it? Share with your friends!

666
1 comment, 666 points

What's Your Reaction?

cute cute
0
cute
scary scary
0
scary
wtf wtf
0
wtf
geek geek
2
geek
WIN WIN
0
WIN
Love Love
0
Love
OMG OMG
0
OMG
Angry Angry
0
Angry
Lol Lol
0
Lol
Test engineer

One Comment

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

Choose A Format
Trivia quiz
Story