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

247
Visualizações
Click on element from a dropdown menu in cypress only if checked

I have the following html code with several elements in a dropdown menu. I only want to click on an item if it is selected (has the class selected) or, as you can see HERE in printscreen, it has that check mark in front of the name. How can I do this with IF in cypress?

I tried something like this but it didn't work:

clickOnDropdown(name: string){
  cy.get('#SelectedPractitionersIds').parent().click(); // #SelectedPractitionersIds is the id for dropdown
  cy.get('.selected').contains(name).then(($button) => { // ".selected" it is a class which appears only when the item is checked and selected
  if ($button.hasClass('check-mark')) {
    cy.get('.selected').contains(name).parent().click();
  } 
}); 

And calling from test like clickOnDropdown('PPNCFPCCPE PPNCFPCCPE')

<div class="dropdown-menu open" style="max-height: 205.2px; overflow: hidden; min-height: 0px;">
   <ul class="dropdown-menu inner selectpicker" role="menu" style="max-height: 193.2px; overflow-y: auto;">
      <li rel="0" class=""><a tabindex="0" class="" style=""><span class="text"> PPNCFPCCPE  PPNCFPCCPE</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
      <li rel="1" class="selected"><a tabindex="0" class="" style=""><span class="text">alex pract2</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
      <li rel="2" class="selected"><a tabindex="0" class="" style=""><span class="text">ppncfpccpe2 ppncfpccpe2</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
   </ul>
</div>

I hope I have provided all the essential details

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

0

The check-mark class you seek is on a descendent of $button, so use .find('.check-mark') in your if() condition.

Unlike .contains() which can search for text in all descendants, .hasClass() only looks at the current element.

const name = "alex pract2";

cy.get(".selected")
  .contains(name)
  .then(($button) => {

    if ($button.find('.check-mark').length) {

      cy.get(".selected").contains(name).parent().click();
      
      //or just
      $button.click()

    }
  });
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