Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
Cypress click on set of elements but not loop forever

I have a few places that I need to click on a number of elements but I don't want it to loop through. I feel that the answer is right in front of me, but cant find the right solution.

Here is one code example which right now is stuck in a forever loop:

for(let n = 1; n <= 19; n++){
        cy.get('li.active > .nav > :nth-child(n) > a').click({multiple: true})
        cy.wait(400)
    }

I have 19 elements all of which are the same just numbered 1-19 and I just want the test to click on them, wait to let it show the page, then click the next one.


Answer

            cy.get('#side-menu > :nth-child(2) > a').click()
        cy.wait(400)
        cy.get('li.active > .nav').each(($ele) => {
  cy.wrap($ele).find('a').click({multiple:true})
})

Also took care of my other for loop that went on for forever and never stopped unless manually stopped like this original issue by replacing it with each() as well.

    cy.get('#side-menu > li > a').each(($ele) => {
        cy.wrap($ele).click({multiple:true})
        })
    ```
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use the each() loop to iterate through your elements and click on the buttons one by one -

cy.get('li.active > .nav').each(($ele) = > {
  cy.wrap($ele).find('a').click()
})
about 4 years ago · Juan Pablo Isaza Relatório

0

If your page refreshes each click, you cannot use Cypress .each() because you will get "detached from DOM" error.

Try a recursive function

it('tests the nav buttons', () => {

  function clickNav(buttonNum) {
    if (buttonNum === 20) return   // finished
    cy.get('li.active > .nav > :nth-child(n) > a').click()
    cy.wait(400)
    clickNav(++buttonNum)   // next
  }

  clickNav(1)
})

But something else is going on, technically your loop should also work.

about 4 years ago · Juan Pablo Isaza Relatório

0

thanks for the solutions - Alpan's worked as such:

            cy.get('#side-menu > :nth-child(2) > a').click()
        cy.wait(400)
        cy.get('li.active > .nav').each(($ele) => {
  cy.wrap($ele).find('a').click({multiple:true})
})

Forgot I had to open the drop down via the nth-child click then added the {multiple:true} inside of click in the each()

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda