This is a migrated thread and some comments may be shown as answers.

Can´t find inner text of an element

9 Answers 426 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Silvio
Top achievements
Rank 1
Silvio asked on 18 Dec 2012, 08:58 AM
Hi,

I´ve got a big problem by getting the inner text of an element. I spent a whole day in trying and letting this in theory simple thing drive me crazy..

<span id="messageText">That´s the text i want to get...</span>

I want to assert this text but always get an empty string. This is my current code:
var frame = browser.WaitForElement(new HtmlFindExpression("id=rightIFrame"), 10000, false);
var span = frame.Find.ByExpression<HtmlSpan>(
"id=rightIFrame");
span.Wait.ForExists();
Assert.AreEqual(
"That´s the text i want to get...", span.TextContent);

Result:
Expected string length x but was 0. Strings differ at index 0.
  Expected: "That´s the text i want to get.."
  But was:  <string.Empty>
  -----------^

I also tried InnerText but it doesn´t change anything, now I just don´t know what to do and hope you can help me.

(Edit: I also already tried Thread.Sleep(), even though it surely hadn´t been a clean and good solution, just to see if the text maybe hasn´t loaded yet, but not even that did change anything..)

Thanks
Kind regards

9 Answers, 1 is accepted

Sort by
0
Boyan Boev
Telerik team
answered on 18 Dec 2012, 11:58 AM
Hello Silvio,

According to the HTML code you have provided to me it is not clear is that span in some frame with ID "rightIFrame"? You can find a frame in our framework via this code:

ArtOfTest.WebAii.Core.Browser frame = ActiveBrowser.Frames.ById("rightIFrame");

In all cases the ID you are using for locating the span element in your code is not correct. The ID of the span is "messageText". Try to change it.

Please try out this slightly different code:

ArtOfTest.WebAii.Core.Browser frame = ActiveBrowser.Frames.ById("rightIFrame");
HtmlSpan span = frame.Find.ByExpression<HtmlSpan>("id=messageText");
span.Wait.ForExists();
Assert.AreEqual("That´s the text i want to get...", span.TextContent);

Let me know if this helps.

Greetings,
Boyan Boev
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Silvio
Top achievements
Rank 1
answered on 18 Dec 2012, 12:10 PM
Oooh sorry..
I quickly wrote this piece of code here in the forum just to show what I mean. ^^ In fact I use That´s surely not my problem. I also switched to the parent-iframe and interacted with some children without a problem. And I can also find the span, but not the text in it..
I tried everything and really just don´t know what else to do.

Edit: I really wonder how I could write such nonsense..^^
0
Boyan Boev
Telerik team
answered on 18 Dec 2012, 12:32 PM
Hello Silvio,

Please try

Assert.AreEqual("That´s the text i want to get...", span.InnerText);
 
instead of 

Assert.AreEqual("That´s the text i want to get...", span.TextContent);

this will get the text if there is some child element.Kind regards,
Boyan Boev
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Silvio
Top achievements
Rank 1
answered on 18 Dec 2012, 01:41 PM
Thanks for your help!
Unfortunately I already tried this, without success. I tried everything possible and nothing worked.

Here are some additional informations (maybe it helps):
The affected control is a modal popup div with the span in it.
First i have to change to the contentFrame, click a button and then the popup shows up. I can find the div and I can also find the span, but when I ask it for its text I get an empty string. No matter how long I wait and what I do.

This is what this popup looks like:

<div>
  <div>
    <img />
    <span>This is the text</span>
  </div>
  <div>
    <button>
      <span />
    </button>
  </div>
</div>


I´ve got no idea what´s the problem and what i could do..


Edit:
The next strange thing: I wanted to get the InnerText from all Elements to see if I can find it there. What I got were the StyleSheets.

This code
rightFrame.Find.AllElements().ToList().ForEach(e => Console.WriteLine(e.InnerText + "\r\n"));


had this result
html,
      body
      {
       
height: 100%;
       
margin: 0;
       
padding: 0;
       
overflow:hidden;
      }

html,
      body
      {
       
height: 100%;
       
margin: 0;
       
padding: 0;
       
overflow:hidden;
      }
0
Boyan Boev
Telerik team
answered on 18 Dec 2012, 02:48 PM
Hi Silvio,

Please double check that you are connecting to this pop up and finding the correct span. There should be no problem to obtain the text if you are able to find the right span. Also you can see its text in the debug mode in the Visual Studio. I recorded a short video as a demonstration.

In order to help you best please if it is possible provide us with a copy of your project and a direct access to the application or point us to a similar public one. You can also record a short video so we can witness this behavior. Jing is a good free screen recorder.

All the best,

Boyan Boev
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Silvio
Top achievements
Rank 1
answered on 18 Dec 2012, 03:32 PM
It works!!
How could i´ve known...

First I connected to the frame and then I clicked a button in this frame. In fact at this time the browser jumped back to the main document (even though the site didn´t change).

Thank you very much for your help!

Kind regards
0
Boyan Boev
Telerik team
answered on 18 Dec 2012, 03:52 PM
Hello Silvio,

I am happy to hear you have solved the issue. 

If you have any further questions please do not hesitate to contact us.

Greetings,
Boyan Boev
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Yatindra
Top achievements
Rank 1
answered on 20 May 2013, 04:56 PM
Hi,

I am also having a similar issue, I have a modal pop-up(not HTML modal) that comes on the screen and takes the precedence. The modal has a div tag inside it and many controls inside the div, so imagine <parent page> <div id="this is a modal"><input type="text" value="abc"/><input type="image" src="" /></div></parent page>. 

My issue is that I am not able to find the elements, Image,Textbox,etc inside the div, and also not able to find the div tag. Please can you help me with this issue.

I am using 2012.2.1420.0version of ArtOfTest.WebAii library.
0
Boyan Boev
Telerik team
answered on 21 May 2013, 01:00 PM
Hi Yatindra,

What do you mean by not HTML modal? What kind of pop up it is?

What code do you use for accessing the element in the pop up?

Modal Popups require special code and should be handled as demonstrated here.

In order to help you best please if it is possible provide us with a copy of your project and a direct access to the application or point us to a similar public one. You can also record a short video so we can witness this behavior. Jing is a good free screen recorder.

Please send us the trace log.

Hope to hear from you soon.

Regards,
Boyan Boev
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
Tags
General Discussions
Asked by
Silvio
Top achievements
Rank 1
Answers by
Boyan Boev
Telerik team
Silvio
Top achievements
Rank 1
Yatindra
Top achievements
Rank 1
Share this question
or