当木偶操纵者最小化时,我的代码不会运行

Had*_*ere 8 javascript puppeteer

我有以下代码,如果我放置最大化的导航窗口,它会很好地工作,但是当我最小化它时,它会停止工作。

更多细节:

当窗口最小化时,“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);
    });
  });
}
Run Code Online (Sandbox Code Playgroud)

tuk*_*kan 4

这是 2018 年的一个相当古老且众所周知的错误(请参阅下面的 github 票证链接)!它连接到 pupeeter 的线程。它与最大化或最小化窗口无关!这里真正的问题是带有 puppeteer 的窗口是否失去焦点。然后窗口暂停(可能实际上放入bg(背景)。

这个问题早已众所周知@github 票证:Puppeteer 在不集中注意力时无法工作

解决方法

解决方法是始终将页面置于最前面。

您必须在代码中实现的示例(来自下面链接的 github 票证中的jtourisNSfherryfherry ):

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);
}
Run Code Online (Sandbox Code Playgroud)