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

128
Visualizações
How do I get the return value from puppeteer?

When the following build runs, I get the error "Error: Evaluation failed: ReferenceError: chunk is not defined" as a console response. In the next line, I can reach chunk[a] as console.log, but why can't I reach it in frame.evaluate?

const puppeteer = require('puppeteer');
const chunk= ["google.com","facebook.com","gmail.com","stackoverflow.com"];

(async () => {
    for(var a=0;a<chunk.length;a++){
      const browser = await puppeteer.launch({headless:false})
      const page = await browser.newPage();
      const iframeHandle = await page.$("frame[name='main']");
      const frame = await iframeHandle.contentFrame();
      console.log(chunk[a]);
      await frame.evaluate(() => {
        if(document.querySelector("#content > table").innerHTML.indexOf(chunk[a]) > -1){
            console.log(chunk[a]);
        }
      });
    }
})()

result : google.com Evaluation failed: ReferenceError: chunk is not defined

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I moved the chunck inside the async and chunk is not defined disappeared. However, this time another error occured regarding the element you want to reach. I tried to correct this as well by adding .waitForSelector and removing page.$ but a third error occured which I believe is because of mispelled element path ("frame[name='main']"). You may change it and try it with the code below:

  (async () => {
    const chunk= ["google.com","facebook.com","gmail.com","stackoverflow.com"];
    for(var a=0;a<chunk.length;a++){
      const browser = await puppeteer.launch({headless:false})
      const page = await browser.newPage();
      const iframeHandle = await page.waitForSelector("frame[name='main']");
      const frame = await iframeHandle.contentFrame();
      console.log(chunk[a]);
      await frame.evaluate(() => {
        if(document.querySelector("#content > table").innerHTML.indexOf(chunk[a]) > -1){
            console.log(chunk[a]);
        }
      });
    }
  })()
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