gpt4 book ai didi

node.js - Puppeteer 无法在谷歌云功能中工作

转载 作者:行者123 更新时间:2023-12-03 07:59:22 25 4
gpt4 key购买 nike

Puppeteer 无法在 Google Cloud 中运行!

图片中出现了 chrome 错误。

enter image description here

我已经在这里实现了建议:Puppeteer error on Heroku: Could not find Chromium

我还尝试将我的 puppeteer 版本从 19.x 降级到 18.x 和 17.x,但没有成功。

我会尝试使用 Playwright,除非这里有人知道如何解决这个问题......

编辑:我尝试使用 puppeteer-chromium-resolver 而不是 puppeteer,但是现在我完全无法在 Node 运行时 16 和 18 上部署我的云功能。

编辑 2:我放弃了 puppeteer-chromium-resolver,转而使用 chrome-aws-lambda,并添加了以下代码片段并部署到 Google 云函数:

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) {
// local execution
return playwright.chromium.launch({});
}
return playwright.chromium.launch({ executablePath });
}
);
})()

语句 console.log("executablePath: ",executablePath); 打印“/tmp/chromium”

但是我收到另一个错误:

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 <launching> /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 }

不知道如何从这里继续......

最终编辑:我尝试在 ECR 中使用 AWS lambda 和 puppeteer,但这也不起作用,所以我在 pythonanywhere.com 上使用 python selenium。整个经历令人沮丧,但至少 Selenium 有效。

以下是 github 的链接,了解更多详细信息:https://github.com/Sparticuz/chromium/issues/29

最佳答案

1。添加 .puppeteerrc.cjs 并更改默认缓存目录。

const { join } = require("path");

/**
* @type {import("puppeteer").Configuration}
*/
module.exports = {
// Changes the cache location for Puppeteer.
cacheDirectory: join(__dirname, ".cache", "puppeteer"),
};

2。添加 .puppeteerrc.cjs 后,必须重新安装 puppeteer 软件包,因此您必须从 GCP 中删除您的函数,然后重新部署您的函数。

3。 Chrome 将安装在您的源目录中,puppeteer 现在可以找到 Chrome 的正确路径。

关于node.js - Puppeteer 无法在谷歌云功能中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74907113/

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