【问题标题】:Puppeteer not running in headless:false modePuppeteer 没有在 headless:false 模式下运行
【发布时间】:2020-05-08 17:43:00
【问题描述】:

我试图在 headless:false 模式下启动 puppeteer。它在我的本地机器上运行,但是当我将它推送到我的服务器并尝试启动它时,我收到了这个错误:

4|scraperP | You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:
4|scraperP | Error: Failed to launch chrome!
4|scraperP | [0620/073557.986542:ERROR:nacl_helper_linux.cc(310)] NaCl helper process running without a sandbox!
4|scraperP | Most likely you need to configure your SUID sandbox correctly
4|scraperP | TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md
4|scraperP |     at onClose (/home/pjotr/scrapermmcreation/node_modules/puppeteer/lib/Launcher.js:285:14)
4|scraperP |     at Interface.helper.addEventListener (/home/pjotr/scrapermmcreation/node_modules/puppeteer/lib/Launcher.js:274:50)
4|scraperP |     at Interface.emit (events.js:165:20)
4|scraperP |     at Interface.close (readline.js:381:8)
4|scraperP |     at Socket.onend (readline.js:154:10)
4|scraperP |     at Socket.emit (events.js:165:20)
4|scraperP |     at endReadableNT (_stream_readable.js:1101:12)
4|scraperP |     at process._tickCallback (internal/process/next_tick.js:152:19)

当我以 headless:true 模式开始时,我没有收到 htis 错误。知道如何解决这个问题吗?这就是我启动 puppeteer 的方式:

var browser = await puppeteer.launch({
   args: [
   '--ignore-certificate-errors',
   '--no-sandbox',
   '--disable-setuid-sandbox',
   '--window-size=1920,1080',
   "--disable-accelerated-2d-canvas",
   "--disable-gpu"],
   ignoreHTTPSErrors: true,
   headless: false,
 });

【问题讨论】:

  • 你能显示代码吗?
  • 我添加了我如何启动它
  • 我认为你没有正确安装chrome。

标签: node.js google-chrome puppeteer headless


【解决方案1】:

1.你必须安装一些 lib 包。

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 \
xvfb x11vnc x11-xkb-utils xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic x11-apps

2.然后用你的脚本启动 xvfb

例子:

xvfb-run --server-args="-screen 0 1024x768x24" npm start

如果您使用 Docker,请遵循此 docker 文件

FROM node:8
RUN apt-get update && \
apt-get install -yq 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 \
xvfb x11vnc x11-xkb-utils xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic x11-apps
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
EXPOSE 8081
CMD xvfb-run --server-args="-screen 0 1024x768x24" npm start

这是一个带有 xvfb 的 puppeteer 示例 https://github.com/nsourov/Puppeteer-with-xvfb

【讨论】:

  • 正在读取包列表...完成构建依赖关系树正在读取状态信息...完成 E:无法找到包 libexpat1 E:无法找到包 libpango-1.0-0 E:找不到任何包包由 glob 'libpango-1.0-0' E: 找不到任何包由正则表达式 'libpango-1.0-0' E: 无法找到包 libxcursor1 E: 无法找到包 ca-certificates E: 无法找到包 xvfb
  • 我尝试安装所有包bt仍然有同样的问题
  • 我按照链接在 Ubuntu 16.04 上成功解决了这个问题。这个答案不值得投反对票。
  • 这不是正确的答案/赞成吗?像魅力一样工作,谢谢
  • 为了让 puppeteer 启动并在 nodeJs digitalocean droplet 上运行,我一直在不断出错,这是最后一个障碍。谢谢!至于 lib 包,如果 ldd chrome | grep not 在您的 node_modules/puppeteer/.../chrome-linux 中没有显示任何内容,那么 sudo apt-get xvfb 就足够了,然后使用 Naimur 所说的 xvfb-run 运行命令。再次感谢!
【解决方案2】:

您可以尝试欺骗 headless chrome 在启用 GPU 的情况下运行:

const browser = await puppeteer.launch({
   headless: false,
   args: ['--headless'],
 })

【讨论】:

    【解决方案3】:

    对我来说它有效:

    const puppeteer = require('puppeteer');
    
    (async () => {
            const browser = await puppeteer.launch({ headless: false });
    
            console.log(await browser.userAgent());
    
            await browser.close();
    })();
    

    【讨论】:

      【解决方案4】:

      以下对我有用

      const puppeteer = require("puppeteer");
      async function getData() {
         const browser = await puppeteer.launch({ headless: false });
      }
      getData();
      
      

      【讨论】:

      • 这专门用于无头模式(无头:true 或简单地省略它)
      猜你喜欢
      • 1970-01-01
      • 2022-06-29
      • 2013-11-13
      • 1970-01-01
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 2011-07-20
      • 1970-01-01
      相关资源
      最近更新 更多