Tuhina
Tuhina

Reputation: 3

Is there any alternative to each() for this scenario

So I have a list of buttons on my webpage and I have to iterate through the list of those buttons and based on the inner text value I have to click the matching button. Now the number of buttons are dynamic, so sometimes it can be 5 or sometimes it can be 6 or something else. Now I have written a code with each() and this works perfectly -

cy.get('buton').each(($ele) => {
    if ($ele.text().trim() === "insurance") {
        cy.wrap($ele).click()
    }
})

My question is are there any other way this logic can be written(without using each).

Upvotes: 0

Views: 50

Answers (2)

user16461847
user16461847

Reputation:

You can select the button directly

cy.contains('button', 'insurance').click()

Upvotes: 1

Alapan Das
Alapan Das

Reputation: 18634

You can use filter() in combination with contains() to get your desired button and click it.

cy.get('button')
  .filter(':contains("insurance")')
  .click()

Upvotes: 0

Related Questions