Captain Comic
Captain Comic

Reputation: 16216

How to find element by text using XPath

I have HTML document downloaded and I need to find element in the tree by its text

snippet, only relevant part...

                                    <div class="py-5 col-12">
                                        <h4>Scale</h4>
                                        <div>
                                            <table class="table table-borderless">
                                                <tbody>
                                                    <tr>
                                                        <th style="width: 300px;">Normalized Volume Percentile</th>
                                                        <td>
                                                            86<span style="position: relative; bottom: 1ex; font-size: 80%;">th</span>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <th>Combined Orderbook Percentile</th>
                                                        <td>
                                                            82<span style="position: relative; bottom: 1ex; font-size: 80%;">th</span>
                                                        </td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>

I need to find th element Normalized Volume Percentile and then extract its td value 86

Search string I use //Normalized Volume Percentile

I was trying the following code and and it does not work

    HtmlWeb web = new HtmlWeb();
    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
    doc = web.Load(requesturl);

    var nodes = doc.DocumentNode.Descendants();
    var body = doc.DocumentNode.SelectNodes("//Normalized Volume Percentile");

Exception

System.Xml.XPath.XPathException: ''//Normalized Volume Percentile' has an invalid token.'

Upvotes: 0

Views: 94

Answers (1)

JaSON
JaSON

Reputation: 4869

If you want to locate node by its text content try

"//th[.='Normalized Volume Percentile']"

To locate td:

"//th[.='Normalized Volume Percentile']/following-sibling::td"

Upvotes: 1

Related Questions