How do I read text from non visible elements with Watir (Ruby)?
For the newer versions of Watir, there is now an Element#text_content
method that does the below JavaScript work for you.
e = d.first
e.text_content
#=> "7"
For Old Versions of Watir (Original Answer):
You can use JavaScript to get this.
e = d.first
browser.execute_script('return arguments[0].textContent', e)
#=> "7"
Note that this would only work for Mozilla-like browsers. For IE-like browsers, you would need to use innerText
. Though if you are using watir-classic it would simply be d.first.innerText
(ie no execute_script
required).
Using attribute_value:
Turns out you can make it simpler by using the attribute_value
method. Seems it can get the same attribute values as javascript.
d.first.attribute_value('textContent')
#=> "7"
Using inner_html
If the element only includes text nodes (ie no elements), you can also use inner_html
:
d.first.inner_html
#=> "7"