【发布时间】:2018-06-09 02:13:09
【问题描述】:
我目前正在做一个项目,我需要构建一个应用程序,该应用程序需要在浏览器中打开一个 URL 才能使用它的某些功能。
为此,我在 nodejs 脚本中使用了puppeteer,以便在服务器端打开浏览器,以便像 api 一样使用它。
这是代码(nodejs):
app.get('/do', (req, res) => {
console.log("ok");
(async() => {
var browser = await puppeteer.launch(
{ args: ['--no-sandbox','--disable-setuid-sandbox'], headless: false });
var page = await browser.newPage();
await page.goto('https://url.com');//i hid the url for personal reason
await page.waitFor(1000); // to wait for 1000ms
await page.waitFor('body div'); // to wait for the 'body div' selector in body
await page.waitFor(() => Math.random() < 0.5); // to wait for the predicate
await page.screenshot({
path: 'public/photo.png'
});
await browser.close();
await res.end('<html><head></title></head><body><h1><img src=photo.png ></img></h1></body></html>');
})();
});
此代码在本地工作,但当我在 heroku 上部署时,它显示此错误:
app[web.1]: /send
应用[web.1]:(节点:4) UnhandledPromiseRejectionWarning:未处理的承诺拒绝 (拒绝 id:1):错误:无法启动 chrome!
应用程序[web.1]: /app/node_modules/puppeteer/.local-chromium/linux-515411/chrome-linux/nacl_helper: 加载共享库时出错:libnss3.so:无法打开共享 目标文件:没有这样的文件或目录
应用程序[web.1]: [21:21:1228/131735.202176:ERROR:nacl_fork_delegate_linux.cc(316)] 错误 NaCl 助手启动确认(0 字节)
应用[web.1]:
应用[web.1]:
应用程序 [web.1]:故障排除: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md
应用[web.1]:
应用[web.1]:(节点:4)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。在 未来,未处理的承诺拒绝将终止 具有非零退出代码的 Node.js 进程。
但是如果我删除 headless: false 它可以工作,但问题是 url 显示了一个温暖的页面,我需要使用像 chrome 或 mozilla 或 safari 这样的浏览器。
我该如何解决这个问题?
【问题讨论】:
标签: javascript node.js google-chrome heroku puppeteer