select table data using Xpath

html html-agility-pack xpath

Question

i used to select table with class name "list". I use this xpath query to select node in Htmlagilitypack

//table[@class="list"]/td/a[@href]

but couldn't get correct output. What's wrong with my xpath code block?

This is the table that i want to grab the data from:

<table class="list">
<tbody>
    <tr>
        <td width="315">
            <b>1</b> <a href="http://www.url.html">data</a><br>
            <b>2</b> <a href="http://www.url.html">data</a><br>
            <b>3</b> <a href="http://www.url.html">data</a><br>
        </td>
    </tr>
</tbody>
</table>

Accepted Answer

I consider that you need to get href values of all the anchors. For that you could use //table[@class='list']//td/a/@href

Your XPath did not work because you are trying to find such a <TD> which is immediate child of <TABLE> which is not the case in the code snippet you've shown. Hence use //TD in your XPath.

Hope this helps.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why