调用 await browser.close() 时出错:(节点:4960)UnhandledPromiseRejectionWarning:page.goto:导航失败,因为页面已关闭

Posted

技术标签:

【中文标题】调用 await browser.close() 时出错:(节点:4960)UnhandledPromiseRejectionWarning:page.goto:导航失败,因为页面已关闭【英文标题】:Error when calling await browser.close(): (node:4960) UnhandledPromiseRejectionWarning: page.goto: Navigation failed because page was closed 【发布时间】:2021-06-25 14:52:52 【问题描述】:

我的目标:

在第一个页面(一个巨大的 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.                                   

如何防止这个错误?

谢谢。

【问题讨论】:

【参考方案1】:

由于page.goto不等待而抛出错误:

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

原因是page.goto 返回了一个 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')

不会出错。

有use cases组合not-awaited page.gotopage.waitForSelector可以在Puppeteer/Playwright中有效,但是当你在goto之后立即进行导航时会导致上述错误。

【讨论】:

以上是关于调用 await browser.close() 时出错:(节点:4960)UnhandledPromiseRejectionWarning:page.goto:导航失败,因为页面已关闭的主要内容,如果未能解决你的问题,请参考以下文章

selenuim

async 与 await 线程调用顺序

C# 异步方法加await调用不就变成同步方法了吗

使用 Async /Await 调用 Web API

使用多个async / await调用构建对象

promise,async await,try catch的问题整理