freshwaterjoe
freshwaterjoe

Reputation: 101

XPath for @class returning multiple @href attributes?

I am attempting to create an XPath that will point to the href attribute of a button (handling pagination).

My XPath is as follows:

//a[contains(@class, 'h-data-pagination__next')]//@href

Which returns the following URL:

http://www.bestcolleges.comhttp//www.bestcolleges.com/database/?pg=2

The issue is that the XPath seems to be adding the new URL attribute to the old attribute instead of replacing it.

Upvotes: 1

Views: 1079

Answers (2)

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

Reputation: 77495

There is more than one <a href= in your document.

So you get an array result. Don't concatenate it, but use iteration, or select e.g. the last href only.

e.g. //a[0]//@href should select the first only

Upvotes: 0

kjhughes
kjhughes

Reputation: 111726

The problem is that your XPath is selecting multiple @href attributes.

Consider the following HTML:

<div>
  <a class="h-data-pagination__next" 
     href="http://www.bestcolleges.com">link 1</a>
  <a class="h-data-pagination__next2" 
     href="http//www.bestcolleges.com/database/?pg=2">link 2</a>
</div>

Your XPath will select both a elements because both have @class attributes that contain the substring, h-data-pagination__next.

To fix

  1. Make your test of @class be more specific:

    //a[@class = 'h-data-pagination__next']/@href
    

    or more robust:

    //a[contains(concat(' ', @class, ' '), ' h-data-pagination__next ')]/@href
    
  2. Or, test another aspect such as the link content:

    //a[. = 'link 1']/@href
    
  3. Or, test a combination of the two.

Upvotes: 1

Related Questions