HtmlAgilityPack id选择

问题描述 投票:-1回答:1

我正在构建一个C#程序,该程序在一个网站上进行,并且需要一个span#id。

这是我的代码:

        var web = new HtmlWeb();
        var doc = web.Load(url);
        System.Threading.Thread.Sleep(21000);
        string adress = doc.DocumentNode
              .SelectSingleNode("//td/span#myId")
              .Attributes["value"].Value;

我总是得到这个错误“system.Xml.XPath.XpathException”:

error system.Xml.XPath.XpathException

谢谢

c# xpath html-agility-pack
1个回答
1
投票

假设您的HTML看起来像这样:

<body>
  <table>
    <tr>
      <td><span id="testedAddress">Some text here</span></td>
    </tr>
  </table>
</body>

然后你想要的XPath是//td/span[@id='testedAddress']

© www.soinside.com 2019 - 2024. All rights reserved.