Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!