last() XPath Function – Part 2
Advanced Usage of last():
Locate the last but one element
→ (//input)[last()-1]
Locate the last but two element
→ (//input)[last()-2]
Locate the middle position element
→ (//input)[last() div 2]
Locate the last element that satisfies a condition
→ (//input)[last()][@type='checkbox']
Explanation:
last()-1, last()-2 help you navigate backward from the end.
div is used for division in XPath.
You can combine last() with conditions to filter specific elements.
Practical Demonstration
Used in real-time Selenium scenarios to dynamically locate elements when positions change.
Download
0 formats
No download links available.
88. last() function in XPath Expressions - Part 2 (Selenium Java 2026) | NatokHD