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

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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..
shouldi go for "second" last li? last()-1?
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.

1 Comment

Creating a proper minimal reproducible example is hard but saves both your time and ours in the long run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.