Bird
0
0

You want to click a link that contains the text 'Learn More' but only if it is inside a div with class 'info'. Which is the correct Cypress command?

hard📝 Application Q8 of 15
Cypress - Selecting Elements
You want to click a link that contains the text 'Learn More' but only if it is inside a div with class 'info'. Which is the correct Cypress command?
Acy.contains('div.info', 'Learn More').click()
Bcy.get('div.info').contains('Learn More').click()
Ccy.contains('Learn More').get('div.info').click()
Dcy.get('div.info').click().contains('Learn More')
Step-by-Step Solution
Solution:
  1. Step 1: Understand chaining to narrow search

    First get the div with class 'info', then find element inside it containing 'Learn More'.
  2. Step 2: Evaluate options

    cy.get('div.info').contains('Learn More').click() correctly chains cy.get('div.info').contains('Learn More'). cy.contains('div.info', 'Learn More').click() tries to use selector in contains incorrectly. cy.contains('Learn More').get('div.info').click() reverses order. cy.get('div.info').click().contains('Learn More') clicks div before contains, which is wrong.
  3. Final Answer:

    cy.get('div.info').contains('Learn More').click() -> Option B
  4. Quick Check:

    Chain get() then contains() to scope search [OK]
Quick Trick: Use cy.get() to scope, then cy.contains() to find text inside [OK]
Common Mistakes:
  • Using selector inside contains() incorrectly
  • Reversing get() and contains() order
  • Clicking before finding element

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes