gpt4 book ai didi

javascript - 最小化 puppeteer 时我的代码不运行

转载 作者:行者123 更新时间:2023-12-05 00:26:46 64 4
gpt4 key购买 nike

我有以下代码,如果我将导航窗口最大化,它运行良好,但是当我最小化它时它停止工作。

更多细节:

当窗口最小化时,“scrollDown & scrollTop”函数停止执行。

'use strict'

const puppeteer = require('puppeteer-core');

const lauchpuppeteer = async () => {
const browser = await puppeteer.launch({
defaultViewport: null,
headless: false,
userDataDir: `${obj.id}`,
args: [`--app=${url}`,
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-renderer-backgrounding',
'--no-sandbox']
});
const [page] = await browser.pages()
await page.waitForNavigation();
await scrollDown(page);
await page.waitFor(5000);
await scrollTop(page);

if (await page.$('li.inline.t-24.t-black.t-normal.break-words') !== null)
fullname = await page.$eval('li.inline.t-24.t-black.t-normal.break-words', el => el.innerText.replace(/\s+/g, ' ').trim());
else console.log('The Full Name was not found.'.red);

}

const scrollTop = async (page) => {
await page.evaluate(_ => {
window.scrollTo(0, 0);
});
}

const scrollDown = async (page) => {
await page.evaluate(async () => {
await new Promise((resolve, reject) => {
var totalHeight = 0;
var distance = 50;
var timer = setInterval(() => {
var scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
if (totalHeight >= scrollHeight) {
clearInterval(timer);
resolve();
}
}, 100);
});
});
}

最佳答案

这是 2018 年以来相当古老且众所周知的错误(请参阅下面的 github 票链接)!这与 pupeeter 的线程有关。是不是 连接到最大化或最小化窗口!这里真正的问题是 puppeteer 的窗口是否失去焦点。然后窗口是 暂停 (也许实际上放入 bg(背景)。
这个问题早就知道了@github票:Puppeteer not working when is not focused .
一种解决方法
解决方法是始终将页面置于最前面。
您必须在您的代码中实现的示例(归功于 jtourisNSfherryfherry 来自下面链接的 github 票证):

await page.goto('https://web.whatsapp.com/',{
waitUntil: 'networkidle2',
timeout: 30000
});

// other code

// Forcing page always on front
while(true) {
page.bringToFront();

//you need create delay() function by yourself
await delay(1000);
}

关于javascript - 最小化 puppeteer 时我的代码不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58138275/

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