How can I know if page is closed in Puppeteer
Asked Answered
K

1

5

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?

Konyn answered 26/9, 2017 at 10:53 Comment(0)
A
15

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 ...
}
Archives answered 9/8, 2018 at 21:32 Comment(1)
This does not work, i always get to this if and app throws that target is closed if (!page.isClosed()) page.close().catch((err) => { console.error(err); console.error('[APP TOP LEVEL] Closing page in while loop failed.'); });Platinotype

© 2022 - 2024 — McMap. All rights reserved.