使用 xfvb headless 运行 Puppeteer:false [英] Running Puppeteer with xfvb headless : false

查看:221
本文介绍了使用 xfvb headless 运行 Puppeteer:false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在无头 Ubuntu 16.04 AWS EC2 实例中运行 Puppeteer,并希望通过 xfvb 使用虚拟显示器运行它.每当我尝试运行它时,我都会继续收到错误:

I am running Puppeteer in a headless Ubuntu 16.04 AWS EC2 instance and would like to run it with a virtual display through xfvb. whenever I try to run it I continue to get the error:

/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 还很陌生,所以我提前为任何格式错误道歉.由于它主要是代码,所以我不被允许发布,所以我添加了这个额外的句子.

I appreciate any help, am pretty new to node.js so I apologize in advance for any format errors. I am not being allowed to post this due to it being mainly code, so I am adding this extra sentence.

推荐答案

您似乎在尝试使用 Xvfb 节点模块.虽然其他答案肯定有效,但这里有一个在 nodejs 中完全有效的片段

You seem to be trying to use the Xvfb node module. While the other answers definitely work, here's a snippet that works fully within 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, //otherwise it defaults to 800x600
        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() 中的错误(和可能的竞争条件)方面并不完美,但它应该可以帮助您入门,并且对我来说它的效果非常一致.

This isn't perfect in terms of handling errors (and possible race conditions) in xvfb.start(), but it should get you started, and it works pretty consistently for me.

记得先安装 Xvfb:sudo apt-get install xvfb(谢谢,@iamfrank)

Remember to install Xvfb first: sudo apt-get install xvfb (Thanks, @iamfrank)

这篇关于使用 xfvb headless 运行 Puppeteer:false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆