0

I want to get the SECOND one with same class. inside there's the link.

<ul>
    <li class="arrow">
        <a href="somelinks.com"> </a>
    </li>
    <li></li>
    <li></li>
    <li class="arrow">
        <a href="someotherlinkstopage.com"> </a>
    </li>
    <li></li>
</ul>

i wanna get the "someotherlinkstopage.com". It's the second last li. should i try second last li? or go with class?

i tried :

//(li[contains(@class,'arrow'])[2]/a/@href

//li[contains(@class, 'arrow')][2]/a/@href

//li[@class='arrow'][2]/a/@href
Michimcchicken
  • 543
  • 1
  • 6
  • 21

2 Answers2

2

You were very close. The following XPath,

(//li[@class = 'arrow'])[2]/a/@href

will select "someotherlinkstopage.com" as requested.

You can also use contains() in this case, but beware of matches against arrows, sparrow, etc. See How can I select an element with multiple classes with Xpath?

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Your second and third tries, as well as the solution I provided, will work on the sample HTML you've shown. If you're not having success with my or your second two solutions, then you've not provided a representative HTML excerpt. – kjhughes May 16 '16 at 18:25
  • i checked html.. only contains two "arrow".. i really dont know why.. and before this i didnt realise it has 2 "arrow", i code //li[contains(@class, 'arrow')]/a/@href , it went to the second page then it stopped.. but it scraped 1st and second page.. – Michimcchicken May 16 '16 at 18:28
  • shouldi go for "second" last li? last()-1? – Michimcchicken May 16 '16 at 18:29
0

i have resolved this using

(//li[@class='arrow'])[last()]/a/@href

i've no idea why [2] can't be used but last() can..

but i know i use last() because there's only 2 class named arrow.. either first or last.

Michimcchicken
  • 543
  • 1
  • 6
  • 21