Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xml

xhtml

xpath

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.

like image 391
flybywire Avatar asked May 27 '09 12:05

flybywire


People also ask

How do I find a link in a text?

A linkText is used to identify the hyperlinks on a web page. It can be determined with the help of an anchor tag (<a>). In order to create the hyperlinks on a web page, you can use anchor tags followed by the linkText. Now, let's examine linkText locator with the help of an example.

Where is XPath for href link in Selenium?

This will get you the generic link: selenium. FindElement(By. XPath("xpath=//a[contains(@href,'listDetails.do')")).


2 Answers

Should be something similar to:

 //a[text()='text_i_want_to_find']/@href 
like image 73
Badaro Avatar answered Sep 28 '22 12:09

Badaro


Too late for you, but for anyone else with the same question...

//a[contains(text(), 'programming')]/@href 

Of course, 'programming' can be any text fragment.

like image 43
MaDeuce Avatar answered Sep 28 '22 13:09

MaDeuce