Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

334
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda