gpt4 book ai didi

node.js - 使用 xvfb headless : false 运行 Puppeteer

转载 作者:行者123 更新时间:2023-12-03 12:16:26 32 4
gpt4 key购买 nike

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

最佳答案

您似乎正在尝试使用 Xvfb Node 模块。虽然其他答案肯定有效,但这是一个在 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() 中的错误(和可能的竞争条件)方面并不完美,但它应该让你开始,它对我来说非常一致。
编辑:记得先安装 Xvfb: sudo apt-get install xvfb (谢谢,@iamfrank)

关于node.js - 使用 xvfb headless : false 运行 Puppeteer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51883055/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com