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

764
Visualizações
Cypress: How to know if element is visible or not in using If condition and xpath?

Cypress: How to know if element is visible or not in using If condition?

The answer of the above question works perfectly for CSS selectors. The answer is

cy.get("body").then($body => {
    if ($body.find("selector_for_your_button").length > 0) {   
    //evaluates as true if button exists at all
        cy.get("selector_for_your_button']").then($header => {
          if ($header.is(':visible')){
            //you get here only if button EXISTS and is VISIBLE
          } else {
            //you get here only if button EXISTS but is INVISIBLE
          }
        });
    } else {
       //you get here if the button DOESN'T EXIST
       assert.isOk('everything','everything is OK');
    }
});

I want to achieve something similar but for XPath. Can anyone help me?

Thank you in advance

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

0

With XPath you can use count()

cy.xpath(`count(//*[@class="${selector}"])`)
  .then(count => {
    if (count > 0) {
      cy.xpath(`//*[@class="${selector}"]`)  // repeat without count
        .click()
    } else {

    }
  })

But I think @jjhelguero's warning still applies. There isn't any retry with conditional testing, so you are losing the biggest advantage of Cypress - reducing flakiness in your tests.

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