首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用时出错等待browser.close():(节点:4960) UnhandledPromiseRejectionWarning: page.goto:导航因页面关闭而失败

调用时出错等待browser.close():(节点:4960) UnhandledPromiseRejectionWarning: page.goto:导航因页面关闭而失败
EN

Stack Overflow用户
提问于 2021-03-29 14:58:56
回答 1查看 4.2K关注 0票数 4

我的目标:

当第一个页面(一个庞大的html)正在加载时,导航到第二个页面。

当前状态:

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

脚本:

代码语言:javascript
复制
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()
})()

错误:

代码语言:javascript
复制
λ 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.                                   

如何防止此错误?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-05 12:50:46

由于没有等待page.goto,引发了错误:

代码语言:javascript
复制
// asynchronous loading
page.goto('http://localhost:1234/index1.htm')

原因是page.goto返回了一个承诺,但是当它等待load事件来解决它时:到第二页(index2.htm)的导航比load事件可能发生的时间早。结果是一个UnhandledPromiseRejectionWarning

[N]avigating to "http://localhost:1234/index1.htm", waiting until "load"也给出了一个提示。

将其改为:

代码语言:javascript
复制
// asynchronous loading
await page.goto('http://localhost:1234/index1.htm')

你不会犯错误的。

use cases组合时,没有期待的page.gotopage.waitForSelector可以有效地在木匠/剧作家,但当你立即进行导航后,它会导致上述错误。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66856542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档