How to Read Text from Non Visible Elements with Watir (Ruby)

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"

using element_name text in rspec match when_visibile

The when_present method returns the element that it as waiting for. The element does not have the feedback method; it is the page-object that does.

You can either get the text of the element that is returned by when_present:

expect(page.feedback_element.when_present.text).to match /Form successfully saved/

Or you can wait for the feedback element and then check the value of the feedback text:

page.feedback_element.when_present
expect(page.feedback).to match /Form successfully saved/

Retrieve text from span element in watir webdriver

It would help if you could point to the page where the problem is reproducible, even if that means that you create a page yourself, put it in dropbox and make it public.

Did you even try to replicate the problem with the html and ruby code that you have posted? I am asking because the problem is not reproducible with the provided code. Take a look yourself:

> browser.ul( :class =>'a').li( :class =>'b').link(:class =>'c').span(:class =>'d')
=> #<Watir::Span:0x..fa66c75ff618434cc located=false selector={:class=>"d", :tag_name=>"span"}>

> browser.ul( :class =>'a').li( :class =>'b').link(:class =>'c').span(:class =>'d').text
=> "text"

As far as I understood, you are surprised that when you provide some code, Watir returns an element, but when you want to execute a method on the element, it complains that it can not find the element.

Take a closer look at the ruby object that represents element on a page:

#<Watir::Span:0x..fa66c75ff618434cc located=false selector={:class=>"d", :tag_name=>"span"}>

Please notice located=false. As far as I know, that means that watir creates a ruby object before trying to access it, so the first line of your code does not raise any exceptions. But then when you try to use the element, watir can not find it on the page and complains.

How to input text to non standard input elements

You can use javascript to do this. The difficulty for me was to handle the nested quotes.

Two pieces of knowledge I had to figure out first before being able to do this w/ regards to nested strings:

a.) regarding how javascript handles nested quotes: http://www.w3schools.com/js/js_strings.asp

b.) on how to deal with nested quotes in ruby: Escaping single and double qoutes from a string in ruby (the %Q operator lets you set whatever you want to begin and end a string)

css_selector = "span[data-offset-key='8c176-0-0']"
b.execute_script(%Q|query="#{css_selector}"|)
b.execute_script("document.querySelector(query).innerHTML='that was tricky'")

Looks like the ability to inject JavaScript using procedures such as this enables you to be able to do just about anything Watir can't do otherwise. Good question, this was a learning experience for me too



Related Topics



Leave a reply



Submit