← Back to library
Cypressselectorsintermediatev10+

Select elements by position and text filter

Target the nth item with .eq() and narrow a set with .filter(':contains(...)').

1it('selects cards by position and text', () => {
2  cy.visit('/products');
3
4  // eq() is 0-indexed — gets the third card
5  cy.get('[data-testid="product-card"]').eq(2).should('be.visible');
6
7  // filter() narrows to only elements containing "Sale"
8  cy.get('[data-testid="product-card"]')
9    .filter(':contains("Sale")')
10    .first()
11    .find('[data-testid="sale-badge"]')
12    .should('be.visible');
13});
#eq#nth#filter#contains