如何知道Puppeteer中的页面是否已关闭

问题描述 投票:0回答:1

当页面存在(或打开)时,我需要在页面上进行一些移动。但是其他异步代码可以随时关闭它。我尝试使用代码,如下所示:

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

如果打开或关闭页面,我怎么知道将此代码传递给测试函数?

async.js puppeteer
1个回答
0
投票

page.isClosed()

您可以使用page.isClosed()来检测Puppeteer中的页面是否已关闭:

if (page.isClosed()) {
  // The page IS closed ...
} else {
  // The page IS NOT closed ...
}
© www.soinside.com 2019 - 2024. All rights reserved.