我试图构建一个快速lambda来打印带有特定URL的pdf,但出现此错误:无法启动chrome! spawn ... node_modules / puppeteer /.../ chrome ...疑难解答
这里提到的方法:https://github.com/GoogleChrome/puppeteer/issues/807#issuecomment-366529860对我没有帮助。
我正在使用的代码:
const browser = await puppeteer.launch({
headless: true,
executablePath: '../../node_modules/puppeteer/.local-chromium/linux-624487/chrome-linux/chrome',
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
try {
const result = await exports.run(browser);
callback(null, result);
} catch (e) {
callback(e);
}
...
exports.run = async (browser) => {
// implement here
// this is sample
const page = await browser.newPage();
await page.goto('https://www.google.com', {
waitUntil: ['domcontentloaded', 'networkidle0']
});
console.log((await page.content()).slice(0, 500));
await page.type('#lst-ib', 'aaaaa');
// avoid to timeout waitForNavigation() after click()
await Promise.all([
// avoid to
// 'Cannot find context with specified id undefined' for localStorage
page.waitForNavigation(),
page.click('[name=btnK]'),
]);
// cookie and localStorage
await page.setCookie({
name: 'name',
value: 'cookieValue'
});
console.log(await page.cookies());
console.log(await page.evaluate(() => {
localStorage.setItem('name', 'localStorageValue');
return localStorage.getItem('name');
}));
const result = await page.pdf({
path: 'hn.pdf',
format: 'A4'
});
console.log(` typeof : ${typeof result}, pdf: ${result}`);
await page.close();
return 'done';
};
答案 0 :(得分:0)
您已经读过this GitHub issue吗?
答案 1 :(得分:0)
我刚刚解决了如何在Firebase上以及本地正确运行Puppeteer的问题,您可以在该github issue上检查我的解决方案。
我在该问题上使用了最突出的注释,以优化Firebase上的运行时,但它也应该与常规的全捆Puppeteer进行相同的工作!