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

163
Views
Bucle a través de identificaciones en titiritero
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; }

Estoy tratando de recorrer el ID "spnGB27562Price". Hay 10 ID (spnGB27562Price0 a spnGB27562Price9). No parece que pueda agregar una variable en la expresión xpath en mi código. ¿Cómo haría esto? Solo estoy tratando de promediar 10 números en una página web.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Simplemente puede usar la sintaxis de back-tick ( ` ) para insertar una variable en su cadena xpath de esta manera:

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

O, alternativamente, puede simplemente agregar dos cadenas junto con el uso de comillas simples ( ' ):

 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!