-1

I just started looking into XPath but am a little confused on the following.

I'm parsing HTML (using the HTML-Agility-Pack in C# / .NET) and I'm able to call the contains function on the "class" attribute for an HTML element to only select the nodes whose class contains a specific value.

For example, the top answer on this SO: How can I match on an attribute that contains a certain string?

But I'm unable to call the contains function on the "href" attribute of the HTML "a" (hyperlink) element.

For example, it seems like a more complicate solution is needed, as per this SO accepted answer: Xpath get a if href contains part of string

And if I try to use the following XPath code, I get the ArgumentNullException: "Value cannot be null" exception.

//td/a[contains(@href, 'part-of-the-hyperlink')]

This is the relevant part of HTML I'm trying to select:

<td>
<a href="/name/part-of-the-hyperlink">
Hugh Dane
</a>          
</td>

What am I missing?

J.D.
  • 954
  • 6
  • 22
  • I'm surprised at the exception. The conformant behaviour when you supply an empty node-set as the first argument of contains() is to return false. You should of course be supplying `@href` rather than `href`. – Michael Kay Jan 13 '19 at 00:32
  • Yes, sorry, I was actually using @href. That was just a typo I now fixed. – J.D. Jan 13 '19 at 03:50

1 Answers1

0

Change

//td/a[contains(href, 'part-of-the-hyperlink')]

to

//td/a[contains(@href, 'part-of-the-hyperlink')]

to test the attribute, href, otherwise you're testing the href child element (which doesn't exist).

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Sorry, my mistake. That was a typo in my question text. I actually was using @href already and still receive the error I stated in my question. – J.D. Jan 13 '19 at 03:51