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

263
Visualizações
How to get name of the button element in Cypress

I need to create a condition that if on a page is a button with the text value "Unblock", it's going to get clicked via Cypress in advance. The clicking part and "if" condition I can manage, I need to know how to get javascript to detect which button has that text value "Unblock", thanks!

cy.get(".scroller").eq(1).find("button").then(($scroller) => {
          let test = $scroller //how to get the text value of the button?
          console.log(test)
        })

    
        
        cy.wait(20000)
        cy.contains("Unblock").click()
        cy.wait(3000)
        cy.contains("Close").click()
        cy.contains("Back to main page").click()
        

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

1.You can use invoke('text') to get the button name:

cy.get('.scroller')
  .eq(1)
  .find('button')
  .invoke('text')
  .then((text) => {
    cy.log(text) //Logs button text
  })
  1. Or You can also use the Jquery text() as well to get the button name:
cy.get('.scroller')
  .eq(1)
  .find('button')
  .then(($scroller) => {
    cy.log($scroller.text()) //Logs button text
  })

So in your case you can apply the if-else like this:

cy.get('.scroller')
  .eq(1)
  .find('button')
  .then(($scroller) => {
    if ($scroller.text().trim() == 'Unblock') {
      //Do Something
    } else {
      //Do Something
    }
  })
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use cy.contains(), since it only returns the one button with the required text.

cy.contains("button", "Unblock")
  .then(($scrollerButton) => {
    console.log($scrollerButton.text())
  })
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