Second Text Match

Imagine we have multiple elements on the page with the same text, and we want to grab the second element with the given text. The command cy.containsopen in new window is great, but it gives us the first element. We need the second. We can use the cy.getopen in new window command with jQuery :containsopen in new window selector that matches the elements by the partial text. Then we can use the cy.eqopen in new window command to pick the element by index.

  <li>Apple pies</li>
  <li>Sour grapes</li>
// cy.contains returns the first match for "Apple"
cy.contains('Apple').should('have.text', 'Apples')
// use jQuery :contains selector to find all elements
// by the partial text match
  .should('have.length', 2)
  // grab the second match
  .should('have.text', 'Apple pies')

Find a video explaining this test here Find Multiple Elements By The Partial Text Matchopen in new window