Uploaded October 2023 | Updated September 2026, 1 week ago
Sometimes finding an item can give a wrong match. In this video, I explain my strategy for finding the right item inside the right parent element. You can either use cy.within or combine selectors to target both the right parent and the item.
cy.get('#items').within(function () {
// there should be several items
cy.get('li').should('have.length.above', 0)
// find the product we are looking for
cy.contains('li', 'Apples')
// and confirm its "data" attributes
.should('have.attr', 'data-product-id', '190')
.and('have.attr', 'data-price', '299')
})
Find the full recipe with more solutions at glebbahmutov.com/cypress-examples
Sometimes finding an item can give a wrong match. In this video, I explain my strategy for finding the right item inside the right parent element. You can either use cy.within or combine selectors to target both the right parent and the item.
cy.get('#items').within(function () {
// there should be several items
cy.get('li').should('have.length.above', 0)
// find the product we are looking for
cy.contains('li', 'Apples')
// and confirm its "data" attributes
.should('have.attr', 'data-product-id', '190')
.and('have.attr', 'data-price', '299')
})
Find the full recipe with more solutions at glebbahmutov.com/cypress-examples


![Use cy.filter Command To Filter Elements By Child Element Presence
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 elements 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 https://github.com/bahmutov/cypress-map
Find this recipe at https://glebbahmutov.com/cypress-examples/ Use cy.filter Command To Filter Elements By Child Element Presence](https://i.ytimg.com/vi/ZC6fXDkPtMI/mqdefault.jpg)







