nth, first, and last Locators
When a locator matches multiple elements, you can select by position:
.first()— first matching element.last()— last matching element.nth(index)— element at specific index (0-based)
On the Radio Buttons page, there are 15 radio buttons in five rows of three. Locators return them in document order, row by row: firstSelect1, secondSelect1, thirdSelect1, firstSelect2, secondSelect2, and so on.
Task:
- Navigate to
https://testkru.com/Elements/RadioButtons - Get all radio buttons
- Print the ID of the first radio using
.first()andgetAttribute('id') - Print the ID of the 5th radio (index 4) using
.nth(4)andgetAttribute('id') - Print the ID of the last radio using
.last()andgetAttribute('id')
Expected output:
First: firstSelect1 Fifth: secondSelect2 Last: thirdSelect5
TestKru