How does xpath specify a class while specifying which one to choose at the same time?

I already know that the way to write a specified class is: / / * [@ class="class"]
and I already know that the way to choose the first one is: / / div [n]
what should be the combination of the two? For example, select the last element of class="class".
ask for advice.

tried to write:

Mar.14,2021

xpath syntax can do what you want without having to recreate the wheel.

for example, locate the last H2 element that contains the class title . Xpath can do this

(//h2[@class="title"])[last()]

Please be sure to use parentheses because [] has a higher priority.

< hr >

by the way, in the Google browser developer tool (console, opens by pressing F12), you can execute xpath queries using $x (.) .

reference

xpath grammar document, https://www.w3schools.com/xml.


modify the answer based on the following answer

// 
//*[@class='class'][last()] // 
(//*[@class='class'])[last()] // div [last-1]
Menu