Bird
0
0

Consider this Cypress code snippet:

medium📝 Debug Q14 of 15
Cypress - Selecting Elements
Consider this Cypress code snippet:
cy.get('button').eq(5).click()

It throws an error: "Cannot read property 'click' of undefined". What is the most likely cause?
AThere are fewer than 6 buttons on the page, so eq(5) returns undefined
BThe syntax of eq() is incorrect; it should be eq[5]
Cclick() cannot be chained after eq()
DThe selector 'button' is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Understand eq() index and element count

    eq(5) tries to select the 6th element (index 5). If fewer than 6 buttons exist, it returns undefined.
  2. Step 2: Analyze error message

    The error "Cannot read property 'click' of undefined" means the element does not exist, so click() fails.
  3. Final Answer:

    There are fewer than 6 buttons on the page, so eq(5) returns undefined -> Option A
  4. Quick Check:

    Index out of range = undefined element [OK]
Quick Trick: Check element count before using eq(index) [OK]
Common Mistakes:
  • Using wrong eq() syntax
  • Assuming click() can't chain after eq()
  • Ignoring zero-based index causing out-of-range

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes