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

189
Vistas
How to iterate selector over puppeter

Dear everybody I have a problem with selector in puppeter.

I have this code.

for(var k= 0 ; k<= 21 ; k++) {
  const text = await page.evaluate(() => {
    document.querySelector(
      'div.ui-table__row:nth-child('+k+') > a:nth-child(1) > div:nth-child(2)'
    ).textContent
  })
  console.log(text);            
}

The problem when I try to execute this snippet is that I don't have defined k but i think that is correct.

 Error: Evaluation failed: ReferenceError: k is not defined

How I can solve this problem ? Regards

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

0

k needs to be explicitly passed/injected into evaluate()

const k = 'foo'
await page.evaluate(k => {...}, k)

Change to:

for(var k= 0 ; k<= 21 ; k++) {
  const text = await page.evaluate((nth) => {
    document.querySelector(
      'div.ui-table__row:nth-child('+nth+') > a:nth-child(1) > div:nth-child(2)'
    ).textContent
  }, k)
  console.log(text);            
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

According to this document, You have to pass the variable as an argument to the page.evaluate like this:

const result = await page.evaluate((x) => { // 1. Define "x" to get value in the step 2
  return Promise.resolve(8 * x); // 3. Return the result
}, 7); // 2. Pass 7 to "x"
console.log(result); // prints "56"

Your snippet will look like this:

for(let k = 0; k <= 21; k++) {
  const text = await page.evaluate((nth) => {
    return document.querySelector(
      'div.ui-table__row:nth-child('+ nth +') > a:nth-child(1) > div:nth-child(2)'
    ).textContent; // Return `textContent` to "out side"
  }, k)
  console.log(text);            
}
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