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

253
Visualizações
A way to "cache" a value in Puppeteer's waitForFunction?

Is there a way to "cache" a value in Puppeteer's browser-based functions?

The callback passed to page.waitForFunction( .. ) is running on every DOMMutation from the puppeteer option polling: "mutation". So it could potentially run 500 times and execute const listEl = document.querySelector("#list") 500 times.

I was wondering if there is a way to cache listEl when it's found so that next time the callback is invoked querySelector("#listEl") does not have to run again and the cached value inside listEl is used.

await page.waitForFunction(
    () => {
      const listEl = document.querySelector("#list");
    },
    {
      polling: "mutation",
    }
  );
})();

The callback inside waitForFunction( ..) has no access to variables outside the function. Because the callback runs (injected) inside Browser context and the outside environment is node.js.

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

0

Well you could use localStorage. As puppeteer is using chromium, localStorage is therefore supported.

await page.evaluate(() => {
  localStorage.setItem('key', 'value');
});

You could even probably pass the localStorage value through different executions on a same domain via userDataDir launch option.

const browser = await puppeteer.launch({
  userDataDir: './temp/',
  // ...
});
about 4 years ago · Juan Pablo Isaza Relatório

0

You could just make it a window property:

window.listEl = window.listEl || document.querySelector("#list")
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