Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I know if page is closed in Puppeteer

I need to do some movements on page while page exists (or is opened). But other async code can close it in any time. I try to use code, like this:

async.whilst(
      function(){ /*TEST function: return true if page is opened or false otherwise*/},
      function (cb){
          (async()=>{
                await page.evaluate(_=>{/*some code*/})
           })();
      },
      callbackopt
 )

How can I know, if page is opened or closed, to pass this code to the test function?

like image 915
Aleksander A Avatar asked Dec 23 '22 13:12

Aleksander A


1 Answers

page.isClosed()

You can use page.isClosed() to detect whether a page is closed in Puppeteer:

if (page.isClosed()) {
  // The page IS closed ...
} else {
  // The page IS NOT closed ...
}
like image 63
Grant Miller Avatar answered Jan 13 '23 13:01

Grant Miller