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

160
Vistas
Looping through id's in puppeteer
let scrapeProduct = async (url) => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto(url);
    for(let i = 0;i < 10; i++) {
        const [el] = await page.$x('//*[@id="spnGB27562Price"]')
        const txt = await el.getProperty('textContent');
        let rawTxt = await txt.jsonValue();
        rawTxt = Number(rawTxt);
        average += rawTxt;
    }
    return average / 10;
}

I'm trying to loop through the ID "spnGB27562Price". There are 10 id's (spnGB27562Price0 to spnGB27562Price9). It doesn't seem like I can add a variable in the xpath expression in my code. How would I go about this? I am just trying to average 10 numbers on a webpage.

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

0

You can simply use the back-tick (`) syntax to insert a variable into your xpath string like so:

  const [el] = await page.$x(`//*[@id="spnGB27562Price${i}"]`);

Or you can alternatively just add two strings together with the use of single quotes ('):

  const [el] = await page.$x('//*[@id="spnGB27562Price' + i + '"]);
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