Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

76
Views
Cómo encontrar uno de los muchos selectores que contienen otro selector anidado

Tengo una página web y hay una lista con varios elementos:

Primer tipo de elemento:

 <li class="comp-data-text"> </li> <span class="imstuck" data-bind="text"> Something </span>

Segundo tipo de elemento:

 <li class="comp-data-text"> </li>

Como puede ver, el segundo no contiene la clase "imstuck" ni el texto "Algo". (este es un ejemplo, en el caso real, la segunda línea de código está anidada más profundamente)

Quiero saber qué elemento contiene o no esta línea de código. Basado en la documentación de Cypress y algunos temas existentes que probé:

 cy.get(...).each(($item) => { if ($item.has('span.imstuck')) { cy.wrap($item).find('.imstuck').should('contain', 'Something') } })

La GUI de Cypress ejecuta el código relacionado con la instrucción if incluso si el elemento no contiene una clase 'imstuck'.

 AssertionError Timed out retrying after 10000ms: Expected to find element: .imstuck, but never found it. Queried from element: <li.comp-data-text>

También encontré otra forma pero también funciona como el ejemplo anterior:

 cy.get(...).each(($item) => { if ($item.hasClass('.imstuck')) { cy.wrap($item).find('.imstuck').should('contain', 'Something') } })

Resultado esperado: si el elemento en particular no contiene una clase '.imstuck', la declaración if no ejecutará la tercera línea de código.

Aparece un error de resultados reales porque cada vez que cypress ejecuta la tercera línea de código, incluso si un elemento en particular no contiene la clase 'imstuck' y cuando cypress intenta encontrar la clase 'imstuck' en la tercera línea, cypress arroja un error.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Agregue .length a su declaración if

 cy.get('li').each(($li) => { if ($li.has('span.imstuck').length) { cy.wrap($item).find('.imstuck').should('contain', 'Something') } })

Pero puede ser mejor mover 'span.imstuck' a un nivel superior. De esta manera, solo obtendrá esos elementos con el lapso.

 cy.get('li span.imstuck').each(($span) => { cy.wrap($span).should('contain', 'Something') }
about 4 years ago · Santiago Trujillo Report

0

Basado en la respuesta de Fody, encontré una forma un poco más simple:

 cy.get('li').each(($li) => { if ($li.find('span.imstuck').length) { cy.wrap($item).should('contain', 'Something') } })

Reemplazo .has() en .find() y luego elimino .find() en la siguiente línea. Funciona bien también.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!