Home > Back-end >  Cypress: Get all elements that have some specific attribute equal to any value
Cypress: Get all elements that have some specific attribute equal to any value

Time:07-18

I am trying to get all elements that just have a specific attribute, doesn't matter what the attribute value is, and count the number of elements found.

for example get all attribute that has "row-index" attribute:

<div row-index="0" style="height: 47px; transform: translateY(0px);">aaa</div>
<div row-index="1" style="height: 47px; transform: translateY(0px);">bbb</div>
<div row-index="3" style="height: 47px; transform: translateY(0px);">ccc</div>

I have tried this code, that doesn't work:

cy.get('div[@row-index]').should('have.length', 3);

CodePudding user response:

It should work without the '@'

cy.get('div[row-index]').should('have.length', 3);

CodePudding user response:

You can also directly use row-index for this:

cy.get('[row-index]').should('have.length', 3);

Another way to assert length:

cy.get('[row-index]').its('length').should('eq', 3);
  • Related