Getting the innertext of a DIV

Class library usage, coding and language questions.
User avatar
Aracknid
Posts: 342
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Getting the innertext of a DIV

Post by Aracknid » Tue Aug 16, 2011 6:27 pm

I'm porting my code from an older automation to to VS 2010 using the Ranorex API.

In my old code using their DIV tag objects, I was able to get inner text of a DIV even if it was for a sub object, like an anchor or span within it.

Using Ranorex, I have a DIV object, but its inner text is nothing (using the Spy tool or programmatically).

Is this a bug or by design?

Here's an example of what I'm seeing. My treeview for my app is on the left. Spy on top. You can see the DIV has 3 child object, one is a Span. The span has the text value "1". I need to use innertext of the DIV to see the Spans value.
treeview.JPG
treeview.JPG (167.05 KiB) Viewed 3976 times

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Getting the innertext of a DIV

Post by Ciege » Tue Aug 16, 2011 7:09 pm

If you have a valid element to your DIV object, and the Span property holds the data you want, then all you need to do is query the Span property of the element for its data.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

User avatar
Aracknid
Posts: 342
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Re: Getting the innertext of a DIV

Post by Aracknid » Tue Aug 16, 2011 7:19 pm

I realize that it's possible to do what you suggest. It's just that it is different than another tool I was using. I just expected the DIV object's inner text to be the "inner text", which turns out in my case to be the value of a span with it. But it is not. So is this a bug?

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Getting the innertext of a DIV

Post by Ciege » Tue Aug 16, 2011 7:27 pm

I see what you are saying now. Sorry for the confusion.
Unfortunately, I cannot answer that question for you.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Getting the innertext of a DIV

Post by Support Team » Wed Aug 17, 2011 9:59 am

Hi,
I was able to get inner text of a DIV even if it was for a sub object
With Ranorex it is able to identify sub objects like the <span> element, so if there is a <span> element in your HTML code Ranorex will show it.
I just expected the DIV object's inner text to be the "inner text"
If there is an element or more elements between like in this case:
<div style="color:#00FF00">
  <h3>This is <span style="color:blue">A</span>
a header</h3>
  <p>This is a paragraph.</p>
</div>
Ranorex will recognize the text as the InnerText of the corresponding HTML element. In this case the InnerText of the <h3> tag is "This is a header", the InnerText of the <span> tag is "A" and the InnerText of the <p> tag is "This is a paragraph" and this is perfectly right.
So if there is a <span> element in your DIV tag and the text is written between this <span> tag then everything works as expected.

Regards,
Markus
Ranorex Support Team
.
Image

User avatar
Aracknid
Posts: 342
Joined: Tue Aug 10, 2010 3:23 pm
Location: Toronto, Ontario, Canada

Re: Getting the innertext of a DIV

Post by Aracknid » Wed Aug 17, 2011 2:10 pm

Well then there is a bug. Look at my screen shot and scroll to the bottom right of the image. You can see the DIV's inner text is blank, even though in the DIV there is a span.

Aracknid.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Getting the innertext of a DIV

Post by Support Team » Wed Aug 17, 2011 2:28 pm

Hi,

I have seen your screen shot but I didn't see that their is a bug, so have you carefully read my post?
In my example there is also no InnerText property for the DIV element, but it is perfectly right.

Regards,
Markus
Ranorex Support Team
.
Image

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Getting the innertext of a DIV

Post by Support Team » Fri Mar 23, 2012 11:41 am

Hi,

To get the whole InnerText of an element you can use regular expressions and the following method:
var divTagMydiv = repo.WebDocument.YourElement;
      String pureInnerText;
      pureInnerText= Regex.Replace(divTagMydiv.GetInnerHtml(), "<.*?>", string.Empty);
      Report.Info("PureInnerText: "+pureInnerText);
Regards,
Markus
Ranorex Support Team
.
Image