Search code examples
htmlxpathweb-scrapinghtml-agility-pack

Searching HTML document by ID using XPATH returns wrong result


So, id like to get this element from line 200

<p id="Para">Hello, how are you.</p>

To do so I am using the XPATH

HtmlDoc.DocumentNode.SelectSingleNode("//*[contains(@id,'Para')]")

However, the node that is returned is not the one I am looking for and instead gets an element before it on line 10

<p id="ParaInstruction">Click here to begin</p>

I think this is because the ids have the first 4 chars in common so it gets the first one it can find. How do I ensure that the node that is returned only has the chars specified in the XPATH.


Solution

  • Change

    //*[contains(@id,'Para')]
    

    to

    //*[@id='Para']
    

    to avoid matching every element whose @id contains a "Para" substring, which is what contains() does -- test for substring containment.