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

97
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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