cypresscypress-each

Cypress: How to click on a array-list element if matching text found


I want to add an item in cart with a matching text like 'cashews'. I tried below code but .click() function is giving error as "bind and event handler to the click javascript event"

cy.get('.products').find('.product').each(($e1, index, $list) => {
  const textveg = $e1.find('h4.product-name').text() {
    if (textveg.includes('Cashews')) {
      $e1.find('.button').click();
    }
  }
})

can someone suggest what can be the possible reason that .click() method is not identified by cypress. I am using cypress version 7


Solution

  • You can use .filter() to find your element and click it:

    cy.get('h4.product-name').filter(':contains("Cashews")').click()