gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:01:38 38 4
gpt4 key购买 nike

我的目标:
在加载第一个页面(一个巨大的 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 返回一个 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当组合未等待 page.gotopage.waitForSelector在 Puppeteer/Playwright 中可能有效,但是当您在 goto 后立即进行导航时,会导致上述错误。

关于javascript - 调用 await browser.close() 时出错 : (node:4960) UnhandledPromiseRejectionWarning: page. goto:导航失败,因为页面已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66856542/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com