robert
robert

Reputation: 31

XPath for attribute based on predecessor's attribute value?

So I have something like this

<img src="/icons/folder.gif" alt="[DIR]"> <a href="convobot-main/">convobot-main/</a>

and I want to select the a href only when the img tag has the attribute alt = "[Dir]". How can I achieve this?

Upvotes: 0

Views: 42

Answers (1)

kjhughes
kjhughes

Reputation: 111630

This XPath,

//img[@alt='[DIR]']/following-sibling::a[1]/@href

will select href attributes of all first a siblings of all img elements with an alt attribute value of '[DIR]'.

Upvotes: 1

Related Questions