Skip to content
Advertisement

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
);

Advertisement

Answer

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")
   }
});
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement