pete pete -4 years ago 52
HTML Question

Select text containig tag with xpath

I need to select text containing an

<a>
tag with xpath but I can't figure out the right way to do this. The result should be "Image Caption And A Link." and the anchor link should be preserved.

<p class="caption"><img src="...">
Image Caption <a href="...">And A Link</a>.
</p>


The last expression I tried was following,

//*[preceding-sibling::img]


but no matter how I do it, I either just select the outer or just the text from from the
<a>
tag.

Answer Source

You can't select what is not there. "Image Caption And A Link." does not exist in this HTML.

<p class="caption"><img src="..."> 
Image Caption <a href="...">And A Link</a>.
</p>

The best you can get is a list of separate nodes - it looks like you want all nodes inside a p.caption except when it's an img. This would work:

//p[contains(@class, 'caption')]/node()[not(self::img)]

and return three nodes:

['\nImage Caption ', <a href="...">And A Link</a>, '.\n']

To guard against matching false positives in CSS classes due to partial matches, use

//p[contains(concat(' ', @class, ' '), ' caption ')]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download