Tom
Tom

Reputation: 34356

xpath find specific link in page

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");

Upvotes: 1

Views: 2329

Answers (4)

Andrew Keith
Andrew Keith

Reputation: 7563

try this

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

Upvotes: 1

AnthonyWJones
AnthonyWJones

Reputation: 189447

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

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

Upvotes: 1

philnash
philnash

Reputation: 73029

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");

Upvotes: 3

rslite
rslite

Reputation: 84673

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");

Upvotes: 1

Related Questions