Bird
0
0

Why does Cypress recommend using data attributes for element selection instead of classes or ids?

hard📝 Conceptual Q10 of 15
Cypress - Selecting Elements
Why does Cypress recommend using data attributes for element selection instead of classes or ids?
ABecause data attributes are less likely to change and do not affect styling
BBecause classes and ids are deprecated in modern HTML
CBecause Cypress cannot select elements by class or id
DBecause data attributes automatically wait for elements to appear
Step-by-Step Solution
Solution:
  1. Step 1: Understand the role of data attributes

    Data attributes are added solely for testing and do not affect page style or behavior.
  2. Step 2: Compare with classes and ids

    Classes and ids may change due to design updates, breaking tests.
  3. Final Answer:

    Because data attributes are less likely to change and do not affect styling -> Option A
  4. Quick Check:

    Data attributes = stable, non-styling selectors [OK]
Quick Trick: Use data attributes to avoid brittle selectors [OK]
Common Mistakes:
  • Thinking classes and ids are deprecated
  • Believing Cypress can't select by class or id
  • Assuming data attributes handle waiting automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes