user20358
user20358

Reputation: 14746

Way of looping through DIVs and picking values from two different elements within

I have an array of DIVs like so

<div id="item-14620">
<div>
<span class=""/>
<table border="0" width="100%" height="250">
<tbody>
<tr align="center">
<tr>
<tr>
<tr>
<tr>
    <td valign="top" title="some data here"></td>
    <td valign="top"><strong>some value here</strong> </td>
    <td valign="top" title="">
    <td valign="top">  </td>
</tr>
<tr>
<tr>
</tbody>
</table>
</div>
<div class="printThisItem">
</div>

another DIV in the same list of DIVs

<div id="item4569" >
<div>
<span class=""/>
<table border="0" width="100%" height="250">
<tbody>
<tr align="center">
<tr>
<tr>
<tr>
    <td valign="top" title="some other data here"></td>
    <td valign="top"> <strong>Some other value</strong> </td>
    <td valign="top" title="">
    <td valign="top">  </td>
</tr>
<tr>
<tr>
<tr>
</tbody>
</table>
</div>
<div class="printThisItem">
</div>

I am using an xpath like this

...FindElements(By.XPath(".//*[starts-with(@id,'item')]/div[1]/table/tbody/tr[4]/td[2]/strong"))

to return a list of elements, but that only gets me values on the 4th row in the list of DIVs. There are some divs that have the required data on the 5th row and some that have it on the 4th row. How do I iterate thru all the divs and get values from row 4 where data is in row 4 and from row 5 when data is in row 5?

I thought of iterating thru the divs twice, once with tr[4] in the XPath and the second time with tr[5], but then I also need the order in which they were found. With doing it one row at a time I get all the tr[4] items in a list followed by all the tr[5] items. But thats not how they are laid out in the page. There could be 3 items on row 4, then the next 2 on row 5 and the next 6 on row 4 again. I need to maintain the order as well.

Im using Selenium 2.53

Upvotes: 1

Views: 581

Answers (2)

bit
bit

Reputation: 4487

This xpath should give you any second td containing strong under any tr

.//*[starts-with(@id,'item')]/div[1]/table/tbody/tr/td[2]/strong

So even if the strong is in any row you will get that.

Upvotes: 1

Grasshopper
Grasshopper

Reputation: 9058

Try this xpath -- //div[contains(@id,'item')]//table//tr[4 or 5]/td"). This will get you data in td tag according to order of display.

Upvotes: 0

Related Questions