Help with xpath

41 views
Skip to first unread message

Wayne Gunn

unread,
Apr 8, 2021, 1:00:21 PM4/8/21
to Selenium Users
I've got a website with a structure that looks like
<table id='container'>
    <tbody>
       <tr>
               <td data-th='Name'>child elements and data</td>
               <td data-th='Age'> child elements and data  </td>
               <td data-th='Sex'> child elements and data  </td>
               <td data-th='Hobby'> child elements and data  </td>
       </tr>
       <tr id='detail-111'>
               <bunch of irrelevant child elements.....>
       </tr>
       <tr>......</tr>
       <tr>
               <td data-th='Name'>child elements and data2</td>
               <td data-th='Age'> child elements and data2  </td>
               <td data-th='Sex'> child elements and data2  </td>
               <td data-th='Hobby'> child elements and data2  </td>
       </tr>
       <tr id='detail-222'>
               <bunch of irrelevant child elements.....>
       </tr>
       <tr>......</tr>
       <tr>
               <td data-th='Name'>child elements and data n</td>
               <td data-th='Age'> child elements and data n  </td>
               <td data-th='Sex'> child elements and data n  </td>
               <td data-th='Hobby'> child elements and data  n</td>
       </tr>
       <tr id='detail-nnn'>
               <bunch of irrelevant child elements.....>
       </tr>
       <tr>......</tr>
    </tbody>
</table>

I wrote this xpath but not getting the desired result: 
objTBody.FindElementsByXpath("//tr/td[@data-th='Name']")

It returns the td element in row 1, but what i really want to get is the parent row, not the name td. The row just doesnt have attributes of its own so I am forced to identify it by attributes of its children.

Another question , how do i return a collection of all direct children. If I have a parent dive with 10 children div elements, each having their own div child elements, and I want a collection of the 10 children of the parents, how do i do that? When i try, the grandchildren get returned. Children in IE doesnt do that.

Thank you

Roger Cook

unread,
Apr 8, 2021, 1:11:19 PM4/8/21
to Selenium Users
Go up one step in the xpath with the '..' path. So your xpath would become "//tr/td[@data-th='Name']/..".

roger

Wayne Gunn

unread,
Apr 8, 2021, 5:32:35 PM4/8/21
to Selenium Users
Thank you. That worked

Jason Liu

unread,
Apr 9, 2021, 1:07:49 AM4/9/21
to Selenium Users

//td[@data-th='Name']/..
Reply all
Reply to author
Forward
0 new messages