Bird
0
0

Given the HTML:

medium📝 Predict Output Q4 of 15
Cypress - Selecting Elements
Given the HTML:
<div id='menu'>
  <ul>
    <li class='item'>Home</li>
    <li class='item'>About</li>
  </ul>
</div>

What will cy.get('#menu').find('.item').should('have.length', 2) assert?
AIt will pass only if the items are direct children of #menu.
BIt will pass because there are 2 elements with class 'item' inside #menu.
CIt will fail because get() does not select the parent correctly.
DIt will fail because find() does not work with classes.
Step-by-Step Solution
Solution:
  1. Step 1: Identify elements inside #menu

    There are two li elements with class 'item' inside the #menu div.
  2. Step 2: Understand cy.find() behavior

    cy.find('.item') finds all descendants with class 'item' inside the selected parent.
  3. Final Answer:

    The assertion will pass because there are 2 matching elements inside #menu. -> Option B
  4. Quick Check:

    find() finds all descendants matching selector [OK]
Quick Trick: find() finds all descendants, not just direct children [OK]
Common Mistakes:
  • Assuming find() only finds direct children
  • Thinking find() ignores class selectors
  • Confusing get() and find() scopes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes