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)
My code is below:
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();
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.
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();
})()
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.
Edit: Remember to install Xvfb first: sudo apt-get install xvfb
(Thanks, @iamfrank)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With