← Back to library
Cypressselectorsintermediatev10+

Select element by data-testid attribute

Use a CSS attribute selector to target data-testid — the most stable selector strategy for production suites.

1it('interacts with elements via data-testid', () => {
2  cy.visit('/dashboard');
3
4  // data-testid never breaks due to copy, style, or structure changes —
5  // the gold standard for resilient selectors
6  cy.get('[data-testid="user-menu"]').should('be.visible');
7  cy.get('[data-testid="logout-btn"]').click();
8
9  cy.url().should('include', '/login');
10});
#data-testid#attribute selector#stable selector#get