Use cy.filter Command To Filter Elements By Child Element Presence @gleb
Use cy.filter Command To Filter Elements By Child Element Presence  @gleb
Uploaded August 2025 | Updated September 2026, 1 week ago
We can use the cy.filter command to filter DOM elements by the presence of some child elements. For example, to find all LI elements that have an element inside with class "new", we can do the following (this example also verifies the element's own text)

// confirm 2 new elements
cy.get('li').filter(':has(.new)').should('have.length', 2)
// confirm the elements without "new" label
// have specific tet
cy.get('li')
.filter(':not(:has(.new))')
.map('childNodes.0.nodeValue')
.mapInvoke('trim')
.should('deep.equal', ['Grapes', 'Pears'])

Note: "map" and "mapInvoke" commands come from the plugin github.com/bahmutov/cypress-map

Find this recipe at glebbahmutov.com/cypress-examples
Use cy.filter Command To Filter Elements By Child Element PresenceCheck N Items Or That Nothing Was FoundAppend An ElementUse Cypress Network Stub With TanStack RouterTest The Window.Print() Method From CypressMake E2E Tests Super Fast With cypress-magic-backend PluginSpying On The Console Log MethodRecord And Replay Several API DefinitionsStore Values Using Should Callback FunctionCypress Flaky Test Exercise: Level 6Cypress Flaky Test Exercise: Level 2Stub The Method That Returns The Data To TanStack Router
gleb bahmutov |

Use cy.filter Command To Filter Elements By Child Element Presence

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER