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

Multiple div's with same ID in my code

1 Answer 17 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Ashok
Top achievements
Rank 1
Ashok asked on 09 Jul 2012, 06:41 PM
Due to some specific reasons, we have to generate a lot of Div tags with same id and class in a table or UL LI . I need help in writing code to find all the occurences of these div elements inside a parent table or UL LI ( parent table ID is known).

Find.ById pattern i guess simply returns me the first occurrence of the div , How can I get the collection of all such elements, traverse through them and verify the inner text of the div elements.

1 Answer, 1 is accepted

Sort by
0
Anthony
Telerik team
answered on 09 Jul 2012, 07:26 PM
Hello Ashok,

Given the following HTML:

<table id="table1">
<tr>
 
<td>
<div id="div1">
<p>Entry 1</p>
</div>
</td>
 
<td>
<div id="div1">
<p>Entry 2</p>
</div>
</td>
 
<td>
<div id="div1">
<p>Entry 3</p>
</div>
</td>
 
</tr>
</table>

Here is how to find each <div> based on the parent <table> and verify the InnerText of each:

HtmlTable table = Find.ById<HtmlTable>("table1");
IList<HtmlDiv> list = table.Find.AllByExpression<HtmlDiv>("tagname=div", "id=div1");
 
foreach (HtmlDiv d in list)
{
    Log.WriteLine(d.InnerText);
    Assert.IsTrue(d.InnerText.Contains("Entry"), "Assert failed!");
}


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