admin管理员组文章数量:1389750
My goal:
Navigate to a second page while the first one (a huge html) is loading.
Current status:
The script works until the end, then await browser.close()
crashes.
The script:
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()
})()
The error:
λ 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 .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.
How to prevent this error?
Thanks.
My goal:
Navigate to a second page while the first one (a huge html) is loading.
Current status:
The script works until the end, then await browser.close()
crashes.
The script:
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()
})()
The error:
λ 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/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.
How to prevent this error?
Thanks.
Share Improve this question asked Mar 29, 2021 at 14:58 LawrenceLawrence 3587 silver badges21 bronze badges1 Answer
Reset to default 5The error is thrown due to page.goto
is not awaited:
// asynchronous loading
page.goto('http://localhost:1234/index1.htm')
The reason is page.goto
returns a Promise but as it waits for the load
event to resolve it: the navigation to the 2nd page (index2.htm
) happens earlier than the load event could happen. So the result is an UnhandledPromiseRejectionWarning
.
[N]avigating to "http://localhost:1234/index1.htm", waiting until "load"
gives a hint about this also.
By changing it to:
// asynchronous loading
await page.goto('http://localhost:1234/index1.htm')
You won't get errors.
There are use cases when bining not-awaited page.goto
and page.waitForSelector
can be valid in Puppeteer/Playwright, but when you immediately do a navigation after goto it causes the above error.
本文标签:
版权声明:本文标题:javascript - Error when calling await browser.close(): (node:4960) UnhandledPromiseRejectionWarning: page.goto: Navigation faile 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744716927a2621458.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论