1

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.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
freshwaterjoe
  • 101
  • 3
  • 16

2 Answers2

1

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.

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
0

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

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194