Search code examples
c#xpathhtml-agility-pack

HtmlAgilityPack id selection


I am building a C# program, which goes on a website and takes a span#id.

Here is my code :

        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;

And I always get this error "system.Xml.XPath.XpathException" :

error system.Xml.XPath.XpathException

Thanks


Solution

  • Assuming that your HTML looks something like this:

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

    Then the XPath you want is //td/span[@id='testedAddress']