如何使用 XPath 通过链接文本查找链接 URL?

我有一个良好的形式 XHTML页。 我想找到一个链接的目的地 URL 时,我有文本被链接。

例子

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

我想要一个 XPath表达式,如果给它 programming questions site,它将给 http://stackoverflow.com,如果我给它 news,它将给 http://cnn.com

167459 次浏览

Should be something similar to:

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

which basically identifies an anchor node <a> that has the text you want, and extracts the href attribute.

Think of the phrase in the square brackets as a WHERE clause in SQL.

So this query says, "select the "href" attribute (@) of an "a" tag that appears anywhere (//), but only where (the bracketed phrase) the textual contents of the "a" tag is equal to 'programming questions site'".

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.

if you are using html agility pack use getattributeValue:

$doc2.DocumentNode.SelectNodes("//div[@class='className']/div[@class='InternalClass']/a[@class='InternalClass']").GetAttributeValue("href","")

For case insensitive contains, use the following:

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

translate converts capital letters in PROGRAMMING to lower case programming.