gpt4 book ai didi

javascript - Puppeteer 没有正常工作(浏览器未聚焦时暂停/卡住)

转载 作者:行者123 更新时间:2023-12-05 06:52:34 24 4
gpt4 key购买 nike

我正在使用 Puppeteer.js 来抓取一些 URL。我使用的是 Puppeteer 的默认 Chromium 浏览器。
一切正常,但问题是,当我运行爬虫脚本并在后台执行其他操作时,焦点不再在 Chromium 浏览器上的 Puppeteer,它不起作用:WAITING元素的时间太长,并中止操作,或者换句话说:puppeteer 暂停(或卡住)。

P.S,我也在使用 puppeteer-extrapuppeteer-extra-plugin-stealth用于高级选项的 NPM 包。

以下是我创建浏览器和页面的方式:

async initiateCrawl(isDisableAsserts) {
// Set the browser.
this.isPlannedClose = false;
const browser = await puppeteerExtra.launch({
headless: false,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--start-maximized',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-renderer-backgrounding'
]
});
const pid = browser.process().pid;
browser.on('disconnected', () => {
systemUtils.killProcess(pid);
if (!this.isPlannedClose) {
systemUtils.exit(Status.BROWSER_CLOSE, Color.RED, 0);
}
});
process.on('SIGINT', () => {
this.close(browser, true);
});
// Set the page and close the first empty tab.
const page = await browser.newPage();
const pages = await browser.pages();
if (pages.length > 1) {
await pages[0].close();
}
await page.setRequestInterception(true);
await page.setJavaScriptEnabled(false);
await page.setDefaultNavigationTimeout(this.timeout);
page.on('request', (request) => {
if (isDisableAsserts && ['image', 'stylesheet', 'font', 'script'].indexOf(request.resourceType()) !== -1) {
request.abort();
} else {
request.continue();
}
});
return {
browser: browser,
page: page
};
}

我已经看过:
https://github.com/puppeteer/puppeteer/issues/3339
https://github.com/GoogleChrome/chrome-launcher/issues/169
https://www.gitmemory.com/issue/GoogleChrome/puppeteer/3339/530620329

不工作的解决方案:

const session = await page.target().createCDPSession();
await session.send('Page.enable');
await session.send('Page.setWebLifecycleState', {state: 'active'});
const chromeArgs = [
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-renderer-backgrounding'
];
var ops = {args:[
'--kiosks',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-renderer-backgrounding',
'--disable-canvas-aa',
'--disable-2d-canvas-clip-aa',
'--disable-gl-drawing-for-tests',
'--disable-dev-shm-usage',
'--no-zygote',
'--use-gl=desktop',
'--enable-webgl',
'--hide-scrollbars',
'--mute-audio',
'--start-maximized',
'--no-first-run',
'--disable-infobars',
'--disable-breakpad',
'--user-data-dir='+tempFolder,
'--no-sandbox',
'--disable-setuid-sandbox'
], headless: false, timeout:0 };
puppeteer = require('puppeteer');
browser = await puppeteer.launch(ops);
page = await browser.newPage();

有没有人以前遇到过这个问题并且知道如何解决这个问题?谢谢。

最佳答案

当我更新到最新的 puppeteer 版本 (9.0.0) 时,我的问题得到了解决。

关于javascript - Puppeteer 没有正常工作(浏览器未聚焦时暂停/卡住),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65932528/

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