Get all table tr data in single HtmlNodeCollection c#

c# html-agility-pack web-scraping

Question

I need to retrieve all tr from all tables using HTML Agility Pack.

HTML:

<section class="content-section" id="more">
    <div class="row">
        <div class="col-xs-6"></div>
        <div class="col-xs-6">
            <h2>Specs</h2>
            <div>
                <div>
                    <table>
                        <thead>
                            <tr><th colspan="2"> test</th></tr>
                        </thead>
                        <tbody>                         
                            <tr><td>2</td><td>b</td></tr>
                            <tr><td>1</td><td>a</td></tr>
                        </tbody>
                    </table>                                    
                    <table>
                        <tbody>
                            <tr><td>3</td><td>c</td></tr>
                            <tr><td>4</td><td>d</td></tr>               
                        </tbody>
                    </table>
                </div>              
            </div>
        </div>      
    </div>  
</section>

C#:

HtmlNodeCollection featuresNode = document.DocumentNode.SelectNodes("//*[@id='more']/div/div[2]/div/div[1]/table/tbody/tr");

I am only able to get first table tr but not getting all two table tr in HtmlNodeCollection.

1
1
9/13/2018 10:23:17 AM

Accepted Answer

To get all tr nodes including the one in thead, update your XPath to:

"//*[@id='more']/div/div[2]/div/div[1]/table//tr"

This simplified XPath should also work:

"//*[@id='more']//tr"

If you want only the tr from tbody, use:

"//*[@id='more']//tbody//tr"

Or excluding tr from thead, use:

"//*[@id='more']//tr[not(ancestor::thead)]"
2
9/13/2018 10:17:12 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow