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

158
Views
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 answers
Answer question

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 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!