4

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

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

我怎么知道页面是打开还是关闭,将此代码传递给测试功能?

4

1 回答 1

10

page.isClosed()

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

if (page.isClosed()) {
  // The page IS closed ...
} else {
  // The page IS NOT closed ...
}
于 2018-08-09T21:32:15.597 回答