Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

350
Vistas
Puppeteer question: How to loop thorough an elementhandle?

I have a function that extracts textContent from a CSS or XPath selector.

Can someone help me to achieve this? I'm confused

const nodes = await page.$$('css selector here');
for (const node of nodes) {
    const stuff = await extractText(page, node, 'css selector to extract text') || null;
}

so it will extract text only if the node has the specified selector, otherwise output as null.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Sample HTML:

 <ul>
 <!-- Nodes you would like to be evaluated -->
 <li class="red">Color Red 1</li> <!-- Node you would like not to extractText and get null as result -->
 <li class="red">Color Red 2</li> <!-- Node you would like not to extractText and get null as result -->
 <li class="blue">Color Blue 1</li> <!-- Node you would like to extractText -->
 <li class="blue">Color Blue 2</li> <!-- Node you would like to extractText -->
 <!-- Nodes you would not like to be evaluated -->
 <li class="green">Color Green 1</li>
 <li class="green">Color Green 2</li>
 <li class="yellow">Color Yellow 1</li>
 <li class="yellow">Color Yellow 2</li>
 </ul>

Puppeteer code:

const puppeteer = require('puppeteer');

const extractText = async (page, node, selector) => {
    // In complex scenarios use nodeClassName will not be enough 
    // and you will need to use the Private API of ElementHandle _remoteObject
   
const nodeClassName = await node.getProperty('className')
const nodeJsonValue = await nodeClassName.jsonValue()
const nodeValueForComparison = '.' + nodeJsonValue

if(nodeValueForComparison === selector){  
    const text = await page.$eval(selector, element => element.innerText)
    console.log(text)
    return text
}

    return null
}

 (async() => {
    const url = 'Your URL'  
    const browser = await puppeteer.launch({headless: false}); 
    const page = await browser.newPage();
    await page.goto('url');
    // Pass the selectors of Nodes you would like to be evaluated
    // separated by a comma
    const nodes = await page.$$('.red, .blue', node => node);
    const result = [];
    for (const node of nodes) {
        const stuff = await extractText(page, node, '.blue');
        result.push(stuff)
    }
    console.log(result)
    await browser.close();
})();

It is working right for me

about 4 years ago · Juan Pablo Isaza Denunciar

0

If I understand correctly, you can try something like 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>
      <p>Text 1.</p>
      <p>Text <span>2</span>.</p>
    </body>
  </html>`;

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

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

  const nodes = await page.$$('p');
  for (const node of nodes) {
    const stuff = await extractText(page, node, 'span') || null;
    console.log(stuff);
  }
} catch (err) { console.error(err); } finally { await browser.close(); }

function extractText(page, node, selector) {
  return page.evaluate(
    (node, selector) => node.querySelector(selector)?.innerText ?? null,
    node,
    selector,
  );
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda