Bird
0
0

You want to click the second-to-last item in a list of elements matched by cy.get('.items'). Which command correctly selects this element?

hard📝 Application Q8 of 15
Cypress - Selecting Elements
You want to click the second-to-last item in a list of elements matched by cy.get('.items'). Which command correctly selects this element?
Acy.get('.items').eq(cy.get('.items').length - 2).click()
Bcy.get('.items').eq(1).click()
Ccy.get('.items').last().prev().click()
Dcy.get('.items').eq(-2).click()
Step-by-Step Solution
Solution:
  1. Step 1: Understand negative index in eq()

    Cypress supports negative indices in eq(), where eq(-1) is last, eq(-2) is second-to-last.
  2. Step 2: Evaluate other options

    eq(1) selects second element, not second-to-last. last().prev() is invalid chaining in Cypress. eq(cy.get('.items').length - 2) is invalid because length is not accessible synchronously.
  3. Final Answer:

    cy.get('.items').eq(-2).click() -> Option D
  4. Quick Check:

    eq(-2) = second-to-last element [OK]
Quick Trick: Use negative index in eq() for elements from the end [OK]
Common Mistakes:
  • Trying to use prev() after last() in Cypress
  • Using positive index instead of negative for second-to-last
  • Attempting to access length synchronously

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes