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

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

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 Relatório

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