Find The Right Item Using The cy.within Command Or The Parent Selector @gleb
Find The Right Item Using The cy.within Command Or The Parent Selector  @gleb
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
Find The Right Item Using The cy.within Command Or The Parent SelectorStub the BeforeUnloadEvent.prototype.preventDefault MethodType Placeholders Into The Form: manpm and cypress-await exampleUse 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 Function
gleb bahmutov |

Find The Right Item Using The cy.within Command Or The Parent Selector

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER