Benja0906 Benja0906 - 1 year ago 72
HTML Question

What is the right XPath expression?

Given the following HTML from a Wiki page:

<a href="/wiki/Aluminium_bromide" title="Aluminium bromide">Aluminium bromide</a> – AlBr

what should the XPath expression be, to be able to return:


I have tried:

string(//li[/a = "Aluminium bromide"]//-)

But it just returns an error.

Answer Source

That's a bit tricky because the way the nodes are nested.

But you could say "all text nodes inside the <li> that contains an <a> with a value of 'Aluminium bromide', except the text node of that <a> itself".

//li[a = 'Aluminium bromide']//text()[not(parent::a)]

and then do some post-processing (join, trim, etc).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download