Bird
0
0

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

hard📝 Application Q15 of 15
Cypress - Selecting Elements
You want to click the second-to-last item in a list of elements matched by cy.get('.item'). Which Cypress command chain correctly achieves this?
Acy.get('.item').eq(cy.get('.item').its('length').then(len => len - 2)).click()
Bcy.get('.item').eq(-2).click()
Ccy.get('.item').eq(-1).click({ force: true })
Dcy.get('.item').eq(cy.get('.item').length - 2).click()
Step-by-Step Solution
Solution:
  1. Step 1: Recall eq() indexing in Cypress

    Cypress eq(n) supports negative indices: eq(-1) is last, eq(-2) is second-to-last.
  2. Step 2: Compare options

    Options A and B have invalid syntax passing chainables to eq(). cy.get('.item').eq(-1).click({ force: true }) uses eq(-1) for last item. cy.get('.item').eq(-2).click() correctly uses eq(-2).
  3. Final Answer:

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

    eq(-2) = second-to-last [OK]
Quick Trick: eq() supports negative indices: -2 for second-to-last [OK]
Common Mistakes:
  • Using negative index in eq() incorrectly (e.g., -1 instead of -2)
  • Trying to subtract length directly without .then()
  • Passing Cypress chainable as eq() argument

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes