Law*_*nce 4 javascript asynchronous node.js promise playwright
我的目标:
\n在加载第一个页面(一个巨大的 html)时导航到第二个页面。
\n当前状态:
\n该脚本一直运行到最后,然后await browser.close()崩溃。
剧本:
\nconst { chromium } = require(\'playwright\');\n\n(async () => {\n const browser = await chromium.launch({ headless: false })\n const page = await browser.newPage()\n\n await page.setViewportSize({ width: 1200, height: 800 })\n\n // asynchronous loading\n page.goto(\'http://localhost:1234/index1.htm\')\n\n // wait for the button that navigates to another page\n await page.waitForSelector(\'#button1\');\n \n // button exists. Click it to navigate to index2.htm\n await page.click(\'#button1\')\n \n // await the #first_span element exists in index2.htm to do something\n await page.waitForSelector(\'#first_span\')\n \n // doing something\n console.log(\'First span is visible. Do something.\')\n \n // finish the browser (this causes an error)\n await browser.close()\n})()\n\nRun Code Online (Sandbox Code Playgroud)\n错误:
\n\xce\xbb node nav.js \nFirst span is visible. Do something. \n(node:6356) UnhandledPromiseRejectionWarning: page.goto: Navigation failed because page was closed! \n=========================== logs =========================== \nnavigating to "http://localhost:1234/index1.htm", waiting until "load" \n============================================================ \nNote: use DEBUG=pw:api environment variable to capture Playwright logs. \nError \n at Object.captureStackTrace (C:\\repositorios\\playwright\\node_modules\\playwright\\lib\\utils\\stackTrace.js:48:19) \n at Connection.sendMessageToServer (C:\\repositorios\\playwright\\node_modules\\playwright\\lib\\client\\connection.js:69:48) \n at Proxy.<anonymous> (C:\\repositorios\\playwright\\node_modules\\playwright\\lib\\client\\channelOwner.js:64:61) \n at C:\\repositorios\\playwright\\node_modules\\playwright\\lib\\client\\frame.js:102:65 \n at Frame._wrapApiCall (C:\\repositorios\\playwright\\node_modules\\playwright\\lib\\client\\channelOwner.js:77:34) \n at Frame.goto (C:\\repositorios\\playwright\\node_modules\\playwright\\lib\\client\\frame.js:100:21) \n at C:\\repositorios\\playwright\\node_modules\\playwright\\lib\\client\\page.js:296:60 \n at Page._attributeToPage (C:\\repositorios\\playwright\\node_modules\\playwright\\lib\\client\\page.js:231:20) \n at Page.goto (C:\\repositorios\\playwright\\node_modules\\playwright\\lib\\client\\page.js:296:21) \n at C:\\repositorios\\playwright\\nav.js:9:8 \n(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\nnate 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) \n(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. \nRun Code Online (Sandbox Code Playgroud)\n如何防止这个错误呢?
\n谢谢。
\npage.goto由于未等待而引发错误:
// asynchronous loading
page.goto('http://localhost:1234/index1.htm')
Run Code Online (Sandbox Code Playgroud)
原因是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')
Run Code Online (Sandbox Code Playgroud)
你不会得到错误。
在某些用例中,组合未等待page.goto且page.waitForSelector在 Puppeteer/Playwright 中可能有效,但是当您在 goto 之后立即进行导航时,会导致上述错误。
| 归档时间: |
|
| 查看次数: |
7175 次 |
| 最近记录: |