Google Cloud Functions中Puppeteer无法运行的问题求助
初始问题
- 在Google Cloud使用Puppeteer时无法运行,Chrome相关错误提示找不到Chromium相关路径(截图略)
- 已尝试的方案:
- 参考Stack Overflow上Heroku环境下Puppeteer找不到Chromium的解决方案
- 将Puppeteer版本从19.x降级到18.x、17.x,均无效
- 当时计划尝试Playwright,寻求可行解决思路
第一次调整尝试
尝试使用puppeteer-chromium-resolver替代Puppeteer,但无法在Node.js 16和18运行时部署云函数
第二次调整尝试
放弃puppeteer-chromium-resolver,改用chrome-aws-lambda,部署到Google Cloud Functions的代码片段如下:
const bundledChromium = require('chrome-aws-lambda'); const playwright = require('playwright-core'); (async () => { const browser = await Promise.resolve(bundledChromium.executablePath).then( (executablePath) => { console.log("executablePath: ", executablePath); if (!executablePath) { // 本地执行逻辑 return playwright.chromium.launch({}); } return playwright.chromium.launch({ executablePath }); } ); })()
- 运行日志显示
executablePath输出为/tmp/chromium,但出现新错误**browserType.launch: spawn EFAULT**,日志详情如下:
2022-12-27 15:12:00.281 HKT
function-1ol6uqbjimrh1 Function execution started
2022-12-27 15:12:00.295 HKT
function-1ol6uqbjimrh1 executablePath: /tmp/chromium
2022-12-27 15:12:00.305 HKT
function-1ol6uqbjimrh1 Function execution took 24 ms, finished with status: 'ok'
2022-12-27 15:12:00.414 HKT
function-1ol6uqbjimrh1 browserType.launch: spawn EFAULT
2022-12-27 15:12:00.414 HKT
function-1ol6uqbjimrh1 =========================== logs ===========================
2022-12-27 15:12:00.414 HKT
function-1ol6uqbjimrh1/tmp/chromium --disable-field-trial-config --disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-back-forward-cache --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-component-update --no-default-browser-check --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose,MediaRouter,DialMediaRouteProvider,AcceptCHFrame,AutoExpandDetailsElement,CertificateTransparencyComponentUpdater,AvoidUnnecessaryBeforeUnloadCheckSync,Translate --allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --no-service-autorun --export-tagged-pdf --headless --hide-scrollbars --mute-audio --blink-settings=primaryHoverType=2,availableHoverTypes=2,primaryPointerType=4,availablePointerTypes=4 --no-sandbox --user-data-dir=/tmp/playwright_chromiumdev_profile-K9OYeC --remote-debugging-pipe --no-startup-window
2022-12-27 15:12:00.414 HKT
function-1ol6uqbjimrh1 ============================================================
2022-12-27 15:12:00.414 HKT
function-1ol6uqbjimrh1 at /workspace/index.js:37:25
2022-12-27 15:12:00.414 HKT
function-1ol6uqbjimrh1 at async main (/workspace/index.js:30:21) {
2022-12-27 15:12:00.414 HKT
function-1ol6uqbjimrh1 name: 'Error'
2022-12-27 15:12:00.414 HKT
function-1ol6uqbjimrh1 }
- 无法继续排查该错误
最终解决方案
尝试在AWS Lambda的ECR中使用Puppeteer也失败,最终转用Python Selenium部署在pythonanywhere.com,可正常运行
更多细节可查看相关GitHub issue
内容的提问来源于stack exchange,提问作者Ayudh

