【问题标题】:Wait for all downloads to complete with Puppeteer?等待 Puppeteer 完成所有下载?
【发布时间】:2022-07-27 02:38:14
【问题描述】:

我尝试使用question 中的代码,但没有成功。

waitForFunction 似乎被跳过并且没有被评估。

chrome://downloads/ 显示,文件仍在下载,脚本结束。

const puppeteer = require('puppeteer');
(async () => {
  const browser = await puppeteer.launch({
     headless: false, slowMo: 100, // Uncomment to visualize test
  });
  const page = await browser.newPage();

  await page.goto('https://speed.hetzner.de/');

  // Resize window to 1588 x 901 and await navigation
  await page.setViewport({ width: 1588, height: 901 });

  // Click on <a> "10MB.bin"
  await page.waitForSelector('[href="10GB.bin"]');
  await page.click('[href="10GB.bin"]');

dmPage = await browser.newPage()
await dmPage.goto('chrome://downloads/')


await dmPage.bringToFront()
await dmPage.waitForFunction(() => {
        // monitoring the state of the first download item
        // if finish than return true; if fail click
        const dm = document.querySelector('downloads-manager').shadowRoot
        const firstItem = dm.querySelector('#frb0')
        if (firstItem) {
            const thatArea = firstItem.shadowRoot.querySelector('.controls')
            const atag = thatArea.querySelector('a')
            if (atag && atag.textContent === 'Show in folder') {
                return true
            }
            const btn = thatArea.querySelector('cr-button')
            if (btn && btn.textContent === 'Retry') {
                btn.click()
            }
        }
    },
    { polling: 'raf', timeout: 0 }, // polling? yes. there is a 'polling: "mutation"' which kind of async
)
console.log('finish')

//   await browser.close();
})();

【问题讨论】:

  • 您将超时设置为 0。您可以尝试增加它还是删除超时设置?
  • 谢谢,两个都试过了 - 没有任何改变。

标签: javascript puppeteer


【解决方案1】:

现在是 2022 年。使用 Playwright 摆脱这种群众。 manage downloads

它还有一个更好的实时检查器来编写选择器,以及带有多个屏幕截图的时间线跟踪报告用于调试

【讨论】:

    猜你喜欢
    • 2022-07-27
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 2023-01-28
    • 2019-03-17
    • 2021-10-29
    • 1970-01-01
    相关资源
    最近更新 更多