Find duplicates

Imagine we have a list and there are two items with the same text (the rest are all different). How do we confirm that there two duplicates?

If we know the duplicate value


In the list example above, there should be two list items with the text "Kiwi". We cannot use the cy.contains command as it returns the first element with the text

cy.contains('li', 'Kiwi').should('have.length', 1)

We can use the jQuery selector :contains instead to find all elements with the given text.

cy.get('li:contains(Kiwi)').should('have.length', 2)

But what about the rest of the items? They should be all unique.

  .then(($el) =>$el, 'innerText'))
  // confirm the list has unique elements
  .then((values) => {
    const distinct = Cypress._.uniq(values)
    expect(distinct, 'all strings are different').to.have.length(

You can watch the above recipe derivation in the video Find Duplicate Text Kiwi And Confirm The Rest Of Elements Are Uniqueopen in new window. If we do not know the duplicate value, see the recipe Checking for duplicates