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

98
Views
Get dataset object from an element in Puppeteer

Say i have this element:

<a href="#" class="employee"
  data-id="123"
  data-name="john doe"
>

I'd like to get the data attributes via dataset. I can use the code below to get an individual data attribute, but if i want to get both data-* attributes, i'd have to scrape twice.

const person = await page.$eval(".employee", (el) =>
  el.getAttribute("data-id")
);

I've tried this, but returns an empty object

const person = await page.$eval(".employee", (el) =>
  el.dataset
);
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Try using el.attributes which will return all the attributes of the element and then you can get the data from the returned value without having to scrape twice.

Or in general try saving the element and then getting the data from it, or use regex to select the attributes that starts with data-

about 4 years ago · Juan Pablo Isaza Report

0

Maybe this:

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();

const html = `
  <!doctype html>
  <html>
    <head><meta charset='UTF-8'><title>Test</title></head>
    <body>
      <a href='#' class='employee' data-id='123' data-name='john doe'>John Doe</a>
    </body>
  </html>`;

try {
  const [page] = await browser.pages();

  await page.goto(`data:text/html,${encodeURIComponent(html)}`);

  const data = await page.evaluate(() => {
    const dataset = document.querySelector('a').dataset;
    return Object.fromEntries(Object.entries(dataset));
  });
  console.log(data); // { id: '123', name: 'john doe' }
} catch (err) { console.error(err); } finally { await browser.close(); }
about 4 years ago · Juan Pablo Isaza Report

0

Managed to accomplish it with this, but still very open to know how to retrieve the dataset object.

const dataset = await page.$eval(".employee", (el) => {
   return {
      id: el.getAttribute("data-id"),
      name: el.getAttribute("data-name")
   }
});
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!