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

332
Visualizações
Cypress - If Else based on Presence of element without throwing 'Expected to find element: but never found it.'

While using Cypress - I would like to understand ways to do action vs not do action based on element found or not. I tried all below with help from Google search - but ended up with just a bunch of hours wasted:

  1. cy.xpath('').then($ele => {}) - throws Timed out retrying after 20000ms: Expected to find element: //div[text()='hello']//div, but never found it.
  2. cy.xpath('').then(ele => { eleLenght = Cypress.$(ele).length}) - throws Timed out retrying after 20000ms: Expected to find element: //div[text()='hello']//div, but never found it.
  3. cy.xpath('').then($ele => {}).catch({}) - catch is not defined
  4. cy.xpath('').its('lenght').then($ele => {}) - throws Timed out retrying after 20000ms: Expected to find element: //div[text()='hello']//div, but never found it.
  5. cy.xpath('').should('be:visisble').then($ele => {}) - throws Timed out retrying after 20000ms: Expected to find element: //div[text()='hello']//div, but never found it.

My case: If an element is present - I want to try to delete it, but if not present - gracefully continue without throwing the annoying error as above!

Can someone please help here? Conditional testing doc provided in cypress does'nt help at all!

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

0

You can easily do it with jquery notation:

if (Cypress.$("#your-element-id").length > 0) {
    //do something
} else {
    //do nothing
}

Have you tried below?

cy.xpath('').should('have.length.gt',0).then($ele => {console.log($ele)})
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