Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read text from non visible elements with Watir (Ruby)?

There is a div on a page that is not visible but has some value I want to capture. Calling text on it returns me an empty string.

How do I get the value displayed without having to deal with the raw html? Can I force .text to return me the actual value regardless of the visiblity of the text in the browser?

irb(main):1341:0> d.first.visible?
=> false

irb(main):1344:0> d.first.html
=> "<div class=\"day\">7</div>"

irb(main):1345:0> d.first.text
=> ""

PS: There are many many divs (the page is caching response and display them accordingly). I considered changing all the display:none in the page or clicking to make them visible but I'd prefer to avoid this if possible. If not possible a solution with changing all the display none would be the preferred work around.

PPS: Damned, I tried to overload the visible? method in the Watir::Element class to always return true, but that didn't do the trick.

irb(main):1502:0> d.first.visible?
=> true

irb(main):1504:0> d.first.text
=> ""
like image 364
Jeremy Avatar asked Feb 07 '13 10:02

Jeremy


1 Answers

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"
like image 186
Justin Ko Avatar answered Sep 27 '22 21:09

Justin Ko