使用无头xfvb运行Puppeteer:错误

时间:2018-08-16 18:11:51

标签: node.js puppeteer

我正在无头Ubuntu 16.04 AWS EC2实例中运行Puppeteer,并希望通过xfvb进行虚拟显示。每当我尝试运行它时,我仍然会收到错误消息:

/home/ubuntu/node_modules/xvfb/index.js:84
throw new Error('Could not start Xvfb.');    
Error: Could not start Xvfb.
at Xvfb.startSync (/home/ubuntu/node_modules/xvfb/index.js:84:17)
at Object.<anonymous> (/home/ubuntu/puppeteer-works.js:39:6)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:266:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:596:3)

我的代码如下:

const puppeteer = require('puppeteer');
const fs = require("fs");
const Xvfb = require('xvfb');
var xvfb = new Xvfb();

var text = fs.readFileSync("proxy.txt").toString('utf-8');
const textByLine = text.split(" ");


const preparePageForTests = async (page) => {
  const userAgent = 'Mozilla/5.0 (X11; Linux x86_64)' +
  'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.39 
Safari/537.36';
  await page.setUserAgent(userAgent);
  await page.evaluateOnNewDocument(() => {
  Object.defineProperty(navigator, 'webdriver', {
    get: () => false,
  });
});
await page.evaluateOnNewDocument(() => {
  window.chrome = {
    runtime: {},
  };
});
await page.evaluateOnNewDocument(() => {
  const originalQuery = window.navigator.permissions.query;
  return window.navigator.permissions.query = (parameters) => (
    parameters.name === 'notifications' ?
      Promise.resolve({ state: Notification.permission }) :
      originalQuery(parameters)
  );  
});
await page.evaluateOnNewDocument(() => {
  Object.defineProperty(navigator, 'plugins', {
    get: () => [1, 2, 3, 4, 5],
  });  
});
}

xvfb.startSync();

(async () => {
  const browser = await puppeteer.launch({
    args: ['--no-sandbox', '--proxy-server='+textByLine[0]],
    headless: true, });
  const page = await browser.newPage();
  page.authenticate({
      username: textByLine[1],
      password: textByLine[2]
  });
  await preparePageForTests(page);

  const testUrl ="https://publicindex.sccourts.org/abbeville/publicindex/";
  await page.goto(testUrl);
  const html = await page.content();
  await page.screenshot({path: 'result.png'});
  await browser.close()
  console.log(html)

})();
xvfb.stopSync();

我非常感谢您的帮助,这对node.js来说还很陌生,因此对于任何格式错误,我深表歉意。由于它主要是代码,因此我不允许发布它,所以我添加了这句话。

3 个答案:

答案 0 :(得分:1)

您似乎正在尝试使用Xvfb节点模块。虽然其他答案肯定有效,但这是一个可在nodejs内完全使用的代码段

const puppeteer = require('puppeteer')
const Xvfb = require('xvfb');

(async () => {
    var xvfb = new Xvfb({
        silent: true,
        xvfb_args: ["-screen", "0", '1280x720x24', "-ac"],
    });
    xvfb.start((err)=>{if (err) console.error(err)})
    const browser = await puppeteer.launch({
        headless: false,
        defaultViewport: null,
        args: ['--no-sandbox', '--start-fullscreen', '--display='+xvfb._display]
        });
    const page = await browser.newPage();
    await page.goto(`https://wikipedia.org`,{waitUntil: 'networkidle2'});
    await page.screenshot({path: 'result.png'});
    await browser.close()
    xvfb.stop();
})

就处理xvfb.start()中的错误(和可能的竞争条件)而言,这不是完美的方法,但它应该可以帮助您入门,并且对我来说一直很稳定。

答案 1 :(得分:0)

xfvb似乎安装不正确

您应该为ubuntu OS安装xvfb(X Windows虚拟帧缓冲区)软件包

$ sudo apt-get update

$ sudo apt-get install xvfb

在后台运行Xvfb并指定显示编号(在我的示例中为10)

$ Xvfb :10 -ac &

将DISPLAY变量设置为您选择的数字

$ export DISPLAY=:10

答案 2 :(得分:0)

如果正确安装了其他必需的软件包和xvfb,请运行以下命令:

xvfb-run -a --server-args="-screen 0 1280x800x24 -ac -nolisten tcp -dpi 96 +extension RANDR" command-that-runs-chrome