遇到的异常情况:在Jenkins中运行puppeteer脚本时,Could not find Chromium(rev. 1095492)(系统为Debian)
我使用 puppeteer 和 lighthouse 开发了一个基于 nodeJS 的项目。我正在使用puppeteer登录到任何需要用户登录的网站。登录后,我导航到任何页面,如我的订单、账户信息、订购页面等。
注意--我在Jenkins上把这个项目作为一个自由风格的项目。
细节中的确切错误- 遇到的异常:Exception encountered: Could not find Chromium (rev. 1095492)。这可能发生在以下两种情况
- 你在运行脚本之前没有进行安装(例如
npm install
),或 - 你的缓存路径配置不正确(是:/root/.cache/puppeteer)。
在调用我的脚本之前,尝试了以下3个脚本的运行--->;
apt-get update
apt-get install -y gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget libgbm-dev
npm install -g
npm run MyScriptName
Package.json--> "devDependencies": { "chromedriver": "^108.0.0", "date-and-time": "^2.4.1", "lighthouse": "^9.6.8", "log4js": "^6.7.1", "puppeteer": "^19.7.1" },
我能够用下面的代码来解决这个问题:
const browserFetcher = puppeteer.createBrowserFetcher();
let revisionInfo = await browserFetcher.download('1095492');
const browser =await puppeteer.launch({
executablePath: revisionInfo.executablePath,
ignoreDefaultArgs: ['--disable-extensions'],
headless: true,
args: ['--no-sandbox', "--disabled-setupid-sandbox"]
});
现在chrome被放置在用户目录"~/.cache/puppeteer"
中。检查该目录的权限,以及它是否属于你正在运行应用程序的用户。
或者通过PUPPETEER_CACHE_DIR
环境变量或puppeteer.config.cjs
配置文件来设置Chromium的下载文件夹。
index.js
const puppeteer = require('puppeteer')
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.helloWorld = async (req, res) => {
await puppeteer.createBrowserFetcher().download(puppeteer.PUPPETEER_REVISIONS.chromium)
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('https://example.com/')
res.send(await page.content())
await browser.close()
}
package.json
{
"name": "sample-http",
"version": "0.0.1",
"dependencies": {
"puppeteer": "^19.7.5"
}
}
它与Google Cloud Functions Node.js 16一起工作。
https://issuetracker.google.com/issues/266279679?pli=1
ES模组
index.mjs
import puppeteer from 'puppeteer'
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
export const helloWorld = async (req, res) => {
await puppeteer.createBrowserFetcher().download(puppeteer.defaultBrowserRevision)
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('https://example.com/')
res.send(await page.content())
await browser.close()
}
package.json
{
"name": "sample-http",
"main": "index.mjs",
"version": "0.0.1",
"dependencies": {
"puppeteer": "^19.7.5"
}
}