NuWin
NuWin

Reputation: 276

Select Node Below Specific Element By ID

I am trying to get an element that is directly below a specific ID.

For example:

<tr>
<td><a id="element1"/></td>
<td>Some Text</td>
</tr>

I am trying to get the inner text of the second td. Using the XPath of the 2nd td is not always correct in my case.

With the website I am parsing this data from; the tr element varies in position. The only way I figured to be able to get the correct 2nd td is if it is directly below the specified id in the a tag that is wrapped between the 1st td.

How can I get the InnerText of the 2nd td ("Some Text") based on the id of the element above it?

Upvotes: 1

Views: 897

Answers (1)

Saeb Amini
Saeb Amini

Reputation: 24459

Here's the xPath way:

doc.DocumentNode.SelectSingleNode("//*[@id='element1']/ancestor::td/following-sibling::td");

Next td sibling of a td parent of an element that has an id of element1.

Upvotes: 2

Related Questions