【发布时间】: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