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

165
Visualizações
Select an item from a list with javascript

I am using javascrypt with selenium. I need to search and select one of the items in this list, but I am unable to select it. I can find it, but not select the element. Please help me, thank you very much

var input = document.evaluate("//li[contains(., 'ATACAMA')]", document, null, XPathResult.ANY_TYPE, null );

var thisregion = input.iterateNext();
thisregion.click();

image

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

You could try the following below:

var input = document.evaluate("//li[text()='ATACAMA']", document, null, XPathResult.ANY_TYPE, null );

var thisregion = input.iterateNext();
thisregion.click();
about 4 years ago · Santiago Gelvez Relatório

0

So I don't have a clue about Xpath, but like you said, there is nothing stopping you from using querySelector or in this case querySelectorAll instead.

Here I create an array of all the li elements and use .find to get the element that has the text "Item 3" in it. I then click the element programmatically. All of the li elements have onClick event inline in the html.

Would this work for you?

let li_item = Array.from(document.querySelectorAll('li'))
              .find(x => x.textContent === 'Item 3');

li_item.click();

function select(element, n) {
  element.style.color = "red";
  console.log(`${n} has been selected!`);
}
<ul>
  <li onClick = "select(this, 1)">Item 1</li>
  <li onClick = "select(this, 2)">Item 2</li>
  <li onClick = "select(this, 3)">Item 3</li>
  <li onClick = "select(this, 4)">Item 4</li>
  <li onClick = "select(this, 5)">Item 5</li>
</ul>

about 4 years ago · Santiago Gelvez 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