如何使用XPath通过文本查找元素

时间:2020-10-12 14:56:00

标签: c# html xpath html-agility-pack

我已经下载了HTML文档,我需要通过其文本在树中找到元素

摘要,只有相关部分...

                                    <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>

我需要找到第 th 个元素归一化体积百分比,然后提取其 td 值86

我使用//Normalized Volume Percentile

的搜索字符串

我正在尝试以下代码,但它不起作用

    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");

例外

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

1 个答案:

答案 0 :(得分:1)

如果要按文本内容查找节点,请尝试

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

要找到td

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