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
=> ""
For the newer versions of Watir, there is now an
Element#text_contentmethod that does the below JavaScript work for you.For Old Versions of Watir (Original Answer):
You can use JavaScript to get this.
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 bed.first.innerText(ie noexecute_scriptrequired).Using attribute_value:
Turns out you can make it simpler by using the
attribute_valuemethod. Seems it can get the same attribute values as javascript.Using inner_html
If the element only includes text nodes (ie no elements), you can also use
inner_html: