Aleksander A
Aleksander A

Reputation: 341

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?

Upvotes: 5

Views: 3779

Answers (1)

Grant Miller
Grant Miller

Reputation: 29009

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 ...
}

Upvotes: 15

Related Questions