Skip to content
CodeKruPro

nth, first, and last Locators

13/70

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:

  1. Navigate to https://testkru.com/Elements/RadioButtons
  2. Get all radio buttons
  3. Print the ID of the first radio using .first() and getAttribute('id')
  4. Print the ID of the 5th radio (index 4) using .nth(4) and getAttribute('id')
  5. Print the ID of the last radio using .last() and getAttribute('id')

Expected output:

First: firstSelect1
Fifth: secondSelect2
Last: thirdSelect5