Puppeteer 无法在谷歌云功能中工作

问题描述 投票:0回答:2

Puppeteer 无法在谷歌云中工作!

图片中就是那个镀铬错误。

我已经在这里实现了建议:Heroku 上的 Puppeteer 错误:找不到 Chromium

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

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

编辑:我尝试使用 puppeteer-chromium-resolver 而不是 puppeteer,但是现在我完全无法在节点运行时 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。整个经历令人沮丧,但至少硒有效。

这里有一个 github 链接,了解更多详细信息:https://github.com/Sparticuz/chromium/issues/29

node.js google-cloud-functions puppeteer
2个回答
5
投票

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 的正确路径。


0
投票

我添加这个答案是因为我一整天都被这个问题逼疯了,并关注了网上的所有内容,最终对我有用的是以下内容。

  1. 我为初始部署添加了“gcp-deploy”:“npm install puppeteer”(必须不存在云功能)
  2. 我在后续部署中删除了它,因为否则它将无法工作。
  3. 我确实有 .puppeteerrc.cjs 文件,其内容与上面提到的相同。
  4. 我没有应该从 node_module/puppeteer 文件夹安装的那个。

也许这对某人有帮助。这对我有用,所以你的里程可能会有所不同。

© www.soinside.com 2019 - 2024. All rights reserved.