Skip to content

How to get count of all elements matching a locator and loop through them #21647

Discussion options

You must be logged in to vote

@jjhelguero thanks for your answer but in my particular case, I don't think it's exactly what I need, as I need to use the index of the highest price dress to select a different element on that dress item.

I ended up using:

let dressCount: number;
cy.get(dressesPage.dressPrices).then((matchingElements) => {
    dressCount = matchingElements.length

    // If there's only 1 dress on the page, return index 0
    if (dressCount === 1) {
        return 0;
    }

    for (let dress = 0; dress < dressCount; dress++) {
        let currentDressPriceString: string = ""
        cy.get(dressesPage.dressPrices).eq(dress).then((currentPriceString) => {
            const currentPriceAsNumber: number = …

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by mathewr94
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants