在AWS Lambda(AL2023)运行Headless Playwright WebKit遇断言错误求助
在AWS Lambda(AL2023)中运行Headless Playwright WebKit时遇到WPEBackend断言失败问题
我正在尝试在运行AL2023系统的AWS Lambda函数中使用Headless Playwright WebKit,已知Playwright官方不支持该环境。
操作步骤
- 在同版本AL2023的EC2实例上通过npm安装
playwright-webkit - 使用
ldd、strace和LD_DEBUG=libs工具分析WebKit二进制文件的系统依赖,将所需依赖库打包为ZIP后上传至Lambda作为层
测试脚本
// script.js const { webkit } = require('playwright'); (async () => { const browser = await webkit.launch({ executablePath: '/home/ec2-user/webkit/bin/MiniBrowser', headless: true, args: ['--automation'] }); const page = await browser.newPage(); await page.goto('https://www.google.com'); console.log(await page.title()); await browser.close(); })();
该脚本在EC2实例上可正常运行,说明依赖库已齐全,但在Lambda中执行时抛出如下错误:
2025-05-08T21:46:30.441Z pw:protocol ◀ RECV { "method": "Playwright.pageProxyCreated", "params": { "browserContextId": "8000000000000002", "pageProxyId": "9" } } 2025-05-08T21:46:30.462Z pw:browser [pid=22][err] MiniBrowser: ../../Source/wpebackend-fdo-1.14.2/src/ws.cpp:408: static Instance &WS::Instance::singleton(): Assertion 'isConstructed()' failed. 2025-05-08T21:46:30.785Z pw:browser [pid=22] <process did exit: exitCode=null, signal=SIGABRT> 2025-05-08T21:46:30.801Z pw:browser [pid=22] starting temporary directories cleanup 2025-05-08T21:46:30.804Z pw:browser [pid=22] finished temporary directories cleanup 2025-05-08T21:46:30.826Z ERROR Unhandled Promise Rejection { "errorType": "Runtime.UnhandledPromiseRejection", "errorMessage": "TargetClosedError: Target page, context or browser has been closed", "reason": { "errorType": "TargetClosedError", "errorMessage": "Target page, context or browser has been closed", "name": "TargetClosedError", "stack": [ "TargetClosedError: Target page, context or browser has been closed", " at WKPage.didClose (/opt/nodejs/node_modules/playwright-core/lib/server/webkit/wkPage.js:299:52)", " at WKBrowser._onDisconnect (/opt/nodejs/node_modules/playwright-core/lib/server/webkit/wkBrowser.js:68:57)", " at WKConnection._onClose (/opt/nodejs/node_modules/playwright-core/lib/server/webkit/wkConnection.js:86:10)", " at Socket.<anonymous> (/opt/nodejs/node_modules/playwright-core/lib/server/pipeTransport.js:40:40)", " at Socket.emit (node:events:536:35)", " at Pipe.<anonymous> (node:net:343:12)", " at Pipe.callbackTrampoline (node:internal/async_hooks:130:17)" ] }, "promise": {}, "stack": [ "Runtime.UnhandledPromiseRejection: TargetClosedError: Target page, context or browser has been closed", " at process.<anonymous> (file:///var/runtime/index.mjs:1276:17)", " at process.emit (node:events:524:28)", " at emitUnhandledRejection (node:internal/process/promises:250:13)", " at throwUnhandledRejectionsMode (node:internal/process/promises:385:19)", " at processPromiseRejections (node:internal/process/promises:470:17)", " at process.processTicksAndRejections (node:internal/process/task_queues:96:32)" ] }
补充信息
- 未在网上找到其他用户遇到此
Assertion 'isConstructed()' failed错误的案例,仅在WPEBackend-fdo仓库看到一个未回复的帖子 - 根据源码,
ws.cpp文件与音视频渲染相关,但我认为无头环境下不应需要该模块 - 已尝试设置以下环境变量来禁用图形渲染,但因缺乏文档不确定是否有效:
LD_LIBRARY_PATH=/tmp/browser/webkit/lib:/tmp/browser/webkit/sys/lib:${LD_LIBRARY_PATH}GIO_EXTRA_MODULES=/tmp/browser/webkit/sys/lib/gioWEBKIT_EXEC_PATH=/tmp/browser/webkit/binWEBKIT_INJECTED_BUNDLE_PATH=/tmp/browser/webkit/libLIBGL_ALWAYS_SOFTWARE=trueWEBKIT_DISABLE_COMPOSITING_MODE=1WEBKIT_DISABLE_WAYLAND=1WEBKIT_DISABLE_WEBGL=1WPE_BACKEND=headlessWPE_INIT_VIEW_BACKEND=headless
恳请各位提供解决建议。
内容的提问来源于stack exchange,提问作者Brandon Tran
相关产品推荐
相关产品推荐

