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

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

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