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

160
Visualizações
Trying to figure out how to use Puppeteer to extract a collection of DOM elements based on a class name

The web page that I am scraping has several DIV elements that have the CSS class name of 's-suggestion'.

I'm looking to get dataset values out of these elements.

I have the following line of code in my script (running on node.js):

const suggestions = await page.$$eval('.s-suggestion',(divs) => divs);

When I look at the value of suggestions.length it is 10.

Now the following code:

        for (let n=0;n<suggestions.length;n++) {
            const suggestion = suggestions[n];
            console.log("n="+n)
            
            // line below outputs value of:   undefined 
            console.log("   - suggestion className: "+suggestion.className);

            // line commented out below is approx. what I'd like to have:
            // const sKeyword = suggestion.dataset.keyword
        } // next n

The values returned for the className are all undefined .

I don't think these objects are DOM nodes!

BTW: These DIVs have the same className in question but not some unique id that I would know before-hand.

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

0

Unfortunately, page.evaluate() and alike ones can only transfer serializable values (roughly, the values JSON can handle). As await page.$$eval('.s-suggestion',(divs) => divs); returns a collection of DOM elements that are not serializable (they contain methods and circular references), each element in the collection is replaced with an empty object or undefined. You need to return either serializable value (for example, an array of texts or attributes) or use something like page.$$(selector) and ElementHandle API.

For your case, try:

const keywords = await page.$$eval(
  '.s-suggestion',
  divs => Array.from(divs, div => div.dataset.keyword),
);
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