How can I find the link URL by link text with XPath?

flybywire picture flybywire · May 27, 2009 · Viewed 149.5k times · Source

I have a well formed XHTML page. I want to find the destination URL of a link when I have the text that is linked.

Example

<a href="http://stackoverflow.com">programming questions site</a>
<a href="http://cnn.com">news</a>

I want an XPath expression such that if given programming questions site it will give http://stackoverflow.com and if I give it news it will give http://cnn.com.

Answer

Badaro picture Badaro · May 27, 2009

Should be something similar to:

//a[text()='text_i_want_to_find']/@href