我想在 AWS Lambda 函数上使用 Node.js 包 Chromeless。我使用两个包: chromless 和 serverless-chrome 在我的本地机器上,我的测试脚本运行良好。当我将它部署到 Lambda 时,我得到 .html() 的空结果结果不是空字符串,它是一个空页面。(<html><head></head><body></body></html>) CloudWatch 日志中没有错误。似乎 chrome 运行良好,但无法加载网站。NodeJS 版本是 8.10,async/await 似乎可以工作。希望有人有想法。
代码:
const launchChrome = require('@serverless-chrome/lambda');
const { Chromeless } = require('chromeless');
let index = async function handler () {
await launchChrome({
port: 9222,
chromeFlags: [
'--window-size=1200,800',
'--disable-gpu',
'--headless'
]
})
.then(async (chrome) =>
{
const chromeless = new Chromeless(
{
launchChrome:false,
cdp:{host: 'localhost', port: 9222, secure: false}
}
);
const html = await chromeless
.goto('http://www.google.com')
.wait(5000)
.html();
console.log(html);
chromeless.end();
})
};
exports.handler = index;
日志:
16:39:44 START RequestId: xxx Version: $LATEST
16:39:52 2018-04-05T16:39:52.163Z xxx <html><head></head><body></body></html>
16:39:52 END RequestId: xxx
16:39:52 REPORT RequestId: xxx
Duration: 7375.83 ms Billed Duration: 7400 ms Memory Size: 576 MB