Bird
0
0

Which of the following is the correct syntax to select an element with class btn-primary using cy.get()?

easy📝 Syntax Q12 of 15
Cypress - Selecting Elements
Which of the following is the correct syntax to select an element with class btn-primary using cy.get()?
Acy.get('#btn-primary')
Bcy.get('btn-primary')
Ccy.get('.btn-primary')
Dcy.get('button .btn-primary')
Step-by-Step Solution
Solution:
  1. Step 1: Understand CSS selector for class

    In CSS, a class selector uses a dot before the class name, so .btn-primary selects elements with that class.
  2. Step 2: Check each option

    cy.get('#btn-primary') uses an ID selector (#), which is wrong here. cy.get('btn-primary') misses the dot, so it looks for a tag named 'btn-primary'. cy.get('button .btn-primary') incorrectly uses a descendant selector (space after button), selecting buttons containing .btn-primary elements.
  3. Final Answer:

    cy.get('.btn-primary') -> Option C
  4. Quick Check:

    Class selector = .classname [OK]
Quick Trick: Use dot (.) before class names in CSS selectors [OK]
Common Mistakes:
  • Using # instead of . for classes
  • Omitting the dot before class name
  • Confusing tag selectors with class selectors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes