Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!