Bird
0
0

Which selector is considered the best practice for selecting a button with the label 'Submit' in Cypress?

easy📝 Conceptual Q2 of 15
Cypress - Selecting Elements
Which selector is considered the best practice for selecting a button with the label 'Submit' in Cypress?
Acy.get('[data-cy=submit-button]')
Bcy.get('button').contains('Submit')
Ccy.get('.btn.submit')
Dcy.get('button:contains("Submit")')
Step-by-Step Solution
Solution:
  1. Step 1: Identify stable selectors

    Using a custom attribute like data-cy is stable and less prone to breakage.
  2. Step 2: Compare other options

    Selectors based on text or classes can break if UI changes or styles update.
  3. Final Answer:

    cy.get('[data-cy=submit-button]') -> Option A
  4. Quick Check:

    Stable attribute selector = D [OK]
Quick Trick: Prefer data attributes over text or class selectors [OK]
Common Mistakes:
  • Using text selectors that break on label changes
  • Using class selectors that change with styling
  • Using complex CSS selectors unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes