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

GetText()

3 Answers 96 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
SD
Top achievements
Rank 1
SD asked on 25 Jun 2010, 12:55 AM
Hi Guys,

I'm trying to retrieve some txt from a web page. The text may change from time to time as it 
indicates a stat etc. so fetching is based on the element's location in the page.  

Does Webaii has a method equivalent to GetText()? I'm used to 'fetch' text using xpath using expressions like
String txt = GetText(xpath); etc. 
I've been trying to use <HtmlTableRow> and <HtmlTable> with no success.

For example, I need to retrieve the Task id value (Which currently is 1081 but may change the next run).

Ta. 
<tr> 
<td rowspan="1"  colspan="1" >Status</td> 
<td rowspan="1"  colspan="1" ></td
<td rowspan="1"  colspan="1" ></td
<td rowspan="1"  colspan="1" ></td
</tr> 
<tr> 
<td rowspan="1"  colspan="1" >Task id</td> 
<td rowspan="1"  colspan="1" > 
<span style="font-weight: bold;" >1081</span> 
</td> 
<td rowspan="1"  colspan="1" ></td
<td rowspan="1"  colspan="1" ></td
</tr> 
<tr> 
<td rowspan="1"  colspan="1" >Task Assigned to</td> 
<td rowspan="1"  colspan="1" > 
<span style="font-weight: bold;" >Tester01</span> 
</td> 
<td rowspan="1"  colspan="1" ></td
<td rowspan="1"  colspan="1" ></td
</tr> 
<tr> 
<td rowspan="1"  colspan="1" >Task Due by</td> 
<td rowspan="1"  colspan="1" > 
<span style="font-weight: bold;" >02/07/2010</span> 
</td> 
</tr> 
 

 

3 Answers, 1 is accepted

Sort by
0
Cody
Telerik team
answered on 02 Jul 2010, 04:32 PM
Hello SD,

I took your HTML snippet and placed it inside of a <table> element and gave it an ID attribute to assist with locating the table.

<table id="StatusTable">
    <tr>
        <td colspan="1" rowspan="1">Status</td>
        <td colspan="1" rowspan="1">Datum 1</td>
        <td colspan="1" rowspan="1">Datum 2</td>
        <td colspan="1" rowspan="1">Datum 3</td>
    </tr>
    <tr>
        <td colspan="1" rowspan="1">Task id</td>
        <td colspan="1" rowspan="1"><span style="font-weight: bold;">1081</span></td>
        <td colspan="1" rowspan="1"></td>
        <td colspan="1" rowspan="1"></td>
    </tr>
    <tr>
        <td colspan="1" rowspan="1">Task Assigned to</td>
        <td colspan="1" rowspan="1"><span style="font-weight: bold;">Tester01</span></td>
        <td colspan="1" rowspan="1"></td>
        <td colspan="1" rowspan="1"></td>
    </tr>
    <tr>
        <td colspan="1" rowspan="1">Task Due by</td>
        <td colspan="1" rowspan="1"><span style="font-weight: bold;">02/07/2010</span>
        </td>
    </tr>
</table>

That allows me to locate the table and then access the desired cell and text property with the table using code like this:

HtmlTable myTable = Find.ById<HtmlTable>("StatusTable");
HtmlTableCell taskIDcell = myTable.Rows[1].Cells[1];
string taskID = taskIDcell.InnerText;

Sincerely yours,
Cody
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
SD
Top achievements
Rank 1
answered on 21 Jul 2010, 03:11 AM
Hi Cody, 

I've tried your code with a minor change (Using ByTagIndex instead of ById):
HtmlTable myTable = Manager.ActiveBrowser.Find.ByTagIndex<HtmlTable>("table", 4);
           HtmlTableCell taskIDcell = myTable.Rows[1].Cells[0];
           string taskID = taskIDcell.InnerText;
           Console.Write(taskID);
The thing is, the above code returns all the text in that table (GUI) while I'm interested only in single values like Task id etc...
 The tested table has 7 rows...Using HtmlTableCell taskIDcell = myTable.Rows[5].Cells[0]; Gives an Index out of range message....


0
Cody
Telerik team
answered on 22 Jul 2010, 09:30 PM
Hello SD,

Try using TextContent in place of InnerText. Here's the difference:

  • TextContent - returns the text associated with the current element only. Does not include any text associated with elements under the current element.
  • InnerText - concatenates all text together starting with the current element and recursively appending all text for all elements under the current element.

As a simple example:

<p>Nam liber <strong>tempor</strong> cum soluta nobis</p>

In the above <p> element:
TextContent = Nam liber cum soluta nobis
InnerText = Nam liber tempor cum soluta nobis

Regards,
Cody
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
General Discussions
Asked by
SD
Top achievements
Rank 1
Answers by
Cody
Telerik team
SD
Top achievements
Rank 1
Share this question
or