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

2

6

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
=> ""
Harkey answered 7/2, 2013 at 10:18 Comment(6)
Are you using watir-classic, watir-webdriver or both? While the behaviour is the same in both gems, I think the workaround would be different.Attemper
gem 'watir-webdriver' using firefoxHarkey
What about parsing that HTML?Stalinist
That's what I do but that's not correct to spread HTML syntax knowledge to the user of this gem. Not to mention maintenance efforts with regex. The lib should really have something like innerHtml that should not care about visible or not, since I can query that myself. The text/value function has some extra built-in intelligence that should not be there.Harkey
@Harkey I agree, maybe @ŽeljkoFilipin will know more.. Regex? https://mcmap.net/q/17499/-regex-match-open-tags-except-xhtml-self-contained-tags/1136008 Use f.e. Nokogiri: value = Nokogiri::HTML("<div class=\"day\">7</div>").text # => "7"Stalinist
Thanks for the suggestion! Everytime I use regex, I end up with 2 problems :)Harkey
A
9

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"
Attemper answered 7/2, 2013 at 17:48 Comment(4)
Clever work around! Thanks Justin. That did the trick indeed. It's quite verbose but works. Thanks. I'll accept the answer if no one come up with a more clever answer, but you got my +1 for sure! Many thanks.Harkey
If you need to do this often, you could always add it as a method to the Element class.Attemper
This is is awesome. I like it.Amandy
@davidXYZ, FYI, the current version of Watir now has an Element#text_content method for doing this (see updated answer).Attemper
D
0

Try using the execute_script method do change the value of "visible" to visible. Something like document.getElementById('id').style.visibility = 'visible' assuming it has an ID. If it does not you can always ask the devs to put a test-id on the element (or just do it yourself).

Defazio answered 7/12, 2016 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.