xpath find specific link in page

孤街醉人 提交于 2020-02-05 13:05:04

问题


I'm trying to get the email to a friend link from this page using xpath.

http://www.guardian.co.uk/education/2009/oct/14/30000-miss-university-place

The link itself is wrapped up in tags like this

            <li><a class="rollover sendlink" href="http://www.guardian.co.uk/email/354237257"  title="Opens an email form" name="&lid={pageToolbox}{Email a friend}&lpos={pageToolbox}{2}"><img src="http://static.guim.co.uk/static/80163/common/images/icon_email-friend.gif" alt="" class="trail-icon" /><span>Send to a friend</span></a></li>

I'm using this for my query, but it's not quite right.

            $links = $xpath->query("//a/span[text()='Send to a friend']/@href");

回答1:


You're trying to get the href of the span there. I think you want

$links = $xpath->query("//a[span/text()='Send to a friend']/@href");



回答2:


You need to use something like this (since href is an attribute of a):

$links = $xpath->query("//a[span/text()='Send to a friend']/@href");



回答3:


The href is an attribute of the anchor hence you need:-

 $links = $xpath->query("//a[span[text()='Send to a friend']]/@href");



回答4:


try this

 $links = $xpath->query("//a[span='Send to a friend']/@href");


来源:https://stackoverflow.com/questions/1570966/xpath-find-specific-link-in-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!