Bird
0
0

You want to verify that a page contains a link with text 'Learn More' inside a <nav> element. Which Cypress command is best?

hard📝 Application Q15 of 15
Cypress - Selecting Elements
You want to verify that a page contains a link with text 'Learn More' inside a <nav> element. Which Cypress command is best?
Acy.get('nav').contains('a', 'Learn More').should('exist')
Bcy.contains('nav a', 'Learn More').click()
Ccy.contains('Learn More').should('be.visible')
Dcy.get('a').contains('nav', 'Learn More')
Step-by-Step Solution
Solution:
  1. Step 1: Narrow search inside <nav> for link text

    Use cy.get('nav') to select the nav element first.
  2. Step 2: Use contains() with selector 'a' and text 'Learn More'

    Calling .contains('a', 'Learn More') inside nav finds the link with that text.
  3. Step 3: Assert existence with should('exist')

    This confirms the link is present inside nav.
  4. Final Answer:

    cy.get('nav').contains('a', 'Learn More').should('exist') -> Option A
  5. Quick Check:

    Use get() then contains() to scope text search [OK]
Quick Trick: Use cy.get() then contains() to scope text search [OK]
Common Mistakes:
  • Using contains() without scoping to nav
  • Trying to chain contains() incorrectly
  • Assuming contains() alone checks element existence

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes