← Back to library
Cypressselectorsadvancedv10+
Narrow a matched set with .filter()
Use .filter() to keep only the elements in a matched set that satisfy a selector or text condition.
1it('targets only sale cards and express radio button', () => {
2 cy.visit('/products');
3
4 // .filter() keeps only elements matching the expression —
5 // uses standard jQuery selector syntax
6 cy.get('[data-testid="product-card"]')
7 .filter(':contains("On Sale")')
8 .should('have.length.greaterThan', 0)
9 .first()
10 .find('[data-testid="sale-badge"]')
11 .should('be.visible');
12
13 // Filter by attribute value
14 cy.visit('/checkout');
15 cy.get('input[type="radio"]')
16 .filter('[value="express"]')
17 .check()
18 .should('be.checked');
19});#filter#contains#narrow#subset#attribute