C# htmlagilitypack XPath Help

c# html-agility-pack xpath

Question

I have this xpath expression that does not work

"//div[child[0]::h4[text()[contains(.,'Dir')]]]/a"

To parse this html:

<div class="txt"> 
      <h4 class="c1"> 
        Dir
      </h4> 
    <a  href="/name/myname/">Bob</a>
</div> 

I am trying to get at the link node (a). There are other html tags in the document with the same div/h4 hierarchy, with the only difference being the innertext of the h4 tag. So how do I check that the div class (1) has a sub h4 node with inner text "dir" AND (2) get the first link node (a). Do not assume the link is the next sibling of h4.

Accepted Answer

Couldn't you just use this xpath?

"//div[h4[contains(text(),'Dir')]]/a"

child[0] is not a valid axis AFAIK so it fails.

I don't know if this alone would satisfy your conditions without seeing a more complete example HTML. But this certainly works for this one.

If it's possible that there are multiple h4 elements within the div and you only want to check the first:

"//div[h4[1][contains(text(),'Dir')]]/a"

Popular Answer

How do I check that the div class (1) has a sub h4 node with inner text "dir" AND (2) get the first link node (a)

use:

"//div[@class='txt' and h4[contains(.,'Dir')]]/a[1]"



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why