调用 await browser.close() 时出错:(node:4960) UnhandledPromiseRejectionWarning: page.goto: Navigation failed because page was closed

Error when calling await browser.close(): (node:4960) UnhandledPromiseRejectionWarning: page.goto: Navigation failed because page was closed

我的目标:

在加载第一个页面(巨大 html)时导航到第二个页面。

当前状态:

脚本运行到最后,然后 await browser.close() 崩溃。

脚本:

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch({ headless: false })
  const page = await browser.newPage()

  await page.setViewportSize({ width: 1200, height: 800 })

  // asynchronous loading
  page.goto('http://localhost:1234/index1.htm')

  // wait for the button that navigates to another page
  await page.waitForSelector('#button1');
  
  // button exists. Click it to navigate to index2.htm
  await page.click('#button1')
  
  // await the #first_span element exists in index2.htm to do something
  await page.waitForSelector('#first_span')
  
  // doing something
  console.log('First span is visible. Do something.')
  
  // finish the browser (this causes an error)
  await browser.close()
})()

错误:

λ node nav.js                                                                                                                                                                                                                                 
First span is visible. Do something.                                                                                                                                                                                                          
(node:6356) UnhandledPromiseRejectionWarning: page.goto: Navigation failed because page was closed!                                                                                                                                           
=========================== logs ===========================                                                                                                                                                                                  
navigating to "http://localhost:1234/index1.htm", waiting until "load"                                                                                                                                                                         
============================================================                                                                                                                                                                                  
Note: use DEBUG=pw:api environment variable to capture Playwright logs.                                                                                                                                                                       
Error                                                                                                                                                                                                                                         
    at Object.captureStackTrace (C:\repositorios\playwright\node_modules\playwright\lib\utils\stackTrace.js:48:19)                                                                                                                            
    at Connection.sendMessageToServer (C:\repositorios\playwright\node_modules\playwright\lib\client\connection.js:69:48)                                                                                                                     
    at Proxy.<anonymous> (C:\repositorios\playwright\node_modules\playwright\lib\client\channelOwner.js:64:61)                                                                                                                                
    at C:\repositorios\playwright\node_modules\playwright\lib\client\frame.js:102:65                                                                                                                                                          
    at Frame._wrapApiCall (C:\repositorios\playwright\node_modules\playwright\lib\client\channelOwner.js:77:34)                                                                                                                               
    at Frame.goto (C:\repositorios\playwright\node_modules\playwright\lib\client\frame.js:100:21)                                                                                                                                             
    at C:\repositorios\playwright\node_modules\playwright\lib\client\page.js:296:60                                                                                                                                                           
    at Page._attributeToPage (C:\repositorios\playwright\node_modules\playwright\lib\client\page.js:231:20)                                                                                                                                   
    at Page.goto (C:\repositorios\playwright\node_modules\playwright\lib\client\page.js:296:21)                                                                                                                                               
    at C:\repositorios\playwright\nav.js:9:8                                                                                                                                                                                                  
(node:6356) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To termi
nate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)                                                 
(node:6356) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.                                   

如何防止这个错误?

谢谢。

由于未等待 page.goto 而引发错误:

// asynchronous loading
page.goto('http://localhost:1234/index1.htm')

原因是 page.goto returns Promise 但是当它等待 load 事件解决它时:导航到第二页 (index2.htm) 发生早于加载事件可能发生。所以结果是 UnhandledPromiseRejectionWarning.
[N]avigating to "http://localhost:1234/index1.htm", waiting until "load" 对此也给出了提示。

通过将其更改为:

// asynchronous loading
await page.goto('http://localhost:1234/index1.htm')

您不会收到错误。

组合未等待的 page.gotopage.waitForSelector 可以在 Puppeteer/Playwright 中有效,但是当您在 goto 后立即进行导航时,它会导致以上错误。