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.

<ul>
  <li>Apples</li>
  <li>Grapes</li>
  <li>Apple pies</li>
  <li>Sour grapes</li>
</ul>
// 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
cy.get('li:contains(Apple)')
  .should('have.length', 2)
  // grab the second match
  .eq(1)
  .should('have.text', 'Apple pies')

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