I used to write like //button[@class='button'][last()-1]
Is it correct or are there any other techniques?
Asked
Active
Viewed 219 times
-4

undetected Selenium
- 183,867
- 41
- 278
- 352
1 Answers
-1
In case of varying number of check boxes, to identify the second last check box you can use the xpath predicates last()
, [last()-1]
, etc as well.
As an example the following are valid xpath:
/bookstore/book[last()]
/bookstore/book[last()-1]
/bookstore/book[last()-2]
Conclusion
Syntactically //button[@class='button'][last()-1]
is a valid xpath.
You can find a relevant discussion in Python Selenium - How can I click on the third last element from a group of elements in a table?
Reference

undetected Selenium
- 183,867
- 41
- 278
- 352