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

180
Visualizações
How to deep copy a page of puppeteer in javascript?

I'm using puppeteer to navigate my website. I want to wait for an api that sometimes gets called and sometimes not. I'm using

await page.waitForResponse((response =>response.url().includes(myurl)), { timeout: 1000 });

to wait for that api. This works fine when the api gets called, but whenever the api doesn't get called, it crashes and the page isn't same anymore. So, I want to deep copy the page so that I can just check for the api via it's copy and even if that page gets damaged. I will have another that I can use.

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

0

I think you don't need to copy your page. That's probably not doable very easy and seems like a bit of overkill. Instead, preventing the page from crashing would be a simpler approach.

Try something like this:

async function waitForApi(url, timeoutMs) {
   try {
       console.log('waiting ', timeoutMs+'ms for special API. url:', url);
       const opts = { timeout: timeoutMs || 1000 };
       await page.waitForResponse(response => response.url().includes(url), opts);
       console.log('Special API was called!.');
       return true;
   } catch(err) {
       console.log('Special Api was appearantly not called. (Or may be failed.. Error:', err);
       return false;
   }
}
 
// example call of waitForApi .. 
const myUrl = '...'
const apiCalled = await waitForApi(myUrl, 1000)

if(apiCalled) {
   // do stuff if you want to..
} else {
   // do stuff if you want to..
}

This should now log if the api was called or not and when needed you can handle the cases differently.

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