element.innerText() VS. element.locate.invoke('innerText')

31 views
Skip to first unread message

Steiny

unread,
Jan 9, 2008, 10:32:35 PM1/9/08
to Watir General
I was trying to scrape the text out of a text area but I knew that it
contained leading carrage returns and new line characters but
innerText() was not showing these. I then tried to invoke innerText on
the ole object for that text area, and that did give me the correct
result.

The output is as follows:

irb(main):069:0> IE.attach(:title,/^RE/).text_fields[3].innerText
=> "----- Original Message -----\r\nFrom: admin\r\nTo: admin\r\nSent:
10/01/2008
15:04:03\r\nImportance: Normal\r\n\r\nDefault message used for
testing"

irb(main):070:0> IE.attach(:title,/
^RE/).text_fields[3].locate.invoke('innerText
')
=> "\r\n\r\n----- Original Message -----\r\nFrom: admin\r\nTo: admin\r
\nSent: 10
/01/2008 15:04:03\r\nImportance: Normal\r\n\r\nDefault message used
for testing"

Is this the desired behaviour or is it a bug?

Bret Pettichord

unread,
Jan 10, 2008, 1:01:05 AM1/10/08
to watir-...@googlegroups.com
Here is the code for Element#text. Element#innerText is a deprecated
alias for this method.

# Return the innerText of the object
# Raise an ObjectNotFound exception if the object cannot be found
def text
assert_exists
return ole_object.innerText.strip
end

I recall my collegue Jason having to write some funky code recently to
work around this behavior, with a multiline text field. I think that
stripping may make sense for some elements (e.g. buttons) but not for
text fields.

What do you all think?

Bret


--
Bret Pettichord
Lead Developer, Watir, http://wtr.rubyforge.org
Blog, http://www.io.com/~wazmo/blog

Steiny

unread,
Jan 13, 2008, 4:52:06 PM1/13/08
to Watir General
Oh ok thanks for the clarification. Hmmm it is a good point, there is
an arguement both ways. I would probably tend to say maybe leave out
the stripping simply to give the programmer the choice, or an
alternative is have two separate methods although it isn't entirely
necessary. But yes often you do only want the bare text and not the
white space that encloses it.
Reply all
Reply to author
Forward
0 new messages