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

407
Visualizações
Select an specific option from Dropdown Menu without values using Puppeteer

I am trying to use puppeteer to select and click an option in a dropdown menu in a navbar.

I already searched. There is always a page.select with the ID of the dropdown menu, but the thing is, there is no value option.

How can i, for example, make puppeteer click on the Tools2 dropdown item?
Or, in a case of a dropdown menu with options that changes if you are a different user, how can i make puppeteer always click on the second option of a dropdown? Can i use CSS to make this work?

Here is the dropdown menu:

<ul class="navbar-nav">
          <li class="nav-item active">
            <a class="nav-link" href="home.html">Home<span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown"
              aria-haspopup="true" aria-expanded="false">
              Tools
            </a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
              <a class="dropdown-item" href="#">Tools1</a>
              <a class="dropdown-item" href="#">Tools2</a>
              <a class="dropdown-item" href="#">Tools3</a>
            </div>
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown"
              aria-haspopup="true" aria-expanded="false">
              User
            </a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
              <a href="#" class="dropdown-item" data-toggle="modal" data-target="#myModal">Create users</a>
              <a method="get" class="dropdown-item" href="/usuario">Manage users</a>
              <a class="dropdown-item" href="#">Profile</a>
              <a method="get" action="/usuario/logout" class="dropdown-item" href="/">Logout</a>
            </div>
          </li>
        </ul>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

CSS selector

If the dropdown items are already in the DOM you can use the nth-child CSS pseudo-class to select a specific one:

await page.click('ul > li:nth-child(2) > div > a:nth-child(2)')
  • li:nth-child(2) => the "Tools" menu (1 would be the "Home", while 3 would be the "User" nav-item)
  • a:nth-child(2) => the second <a> element within the dropdown, "Tools2"

In case these list items are dynamically generated then you first need to open the dropdown:

await page.click('ul > li:nth-child(2)')

Then proceed with selecting the 2nd <a> element.

XPath selector

If the text content is more reliable than the (second) position within the dropdown then you can also use XPath's contains() function to detect the element, return it as an element handle then click it :

const [item] = await page.$x('//a[contains(text(), "Tools2")]')
await item.click()

Note: $x returns an array of matched items, hence you either need to use an index of the item or use ES6 destructuring as in my example.

$$eval approach

You can also use page.$$eval to click the item based on its index ([1] will be the second element within the array of dropdown elements):

await page.$$eval('ul > li:nth-child(2) > div > a', (elems) => elems[1].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