我想将for
循环变量传递给page.evaluate中的puppeteer,以获取画廊的所有规格。但这是行不通的。它总是报告UnhandledPromiseRejectionWarning: Error: Evaluation failed
。
以下是我的代码示例:
const puppeteer = require('puppeteer-core');
(async () => {
const browser = await puppeteer.launch({executablePath:'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'});
const page = await browser.newPage();
await page.goto('D:\\Code Workplace\\Javascripts\\TurnPage\\index.html', {waitUntil: 'networkidle2'});
for (let index = 0; index < 4; index++) {
await page.evaluateHandle((index) => {
$('.flipbook').turn('page', index);
}, index);
}
await sleep(300);
await page.emulateMedia('print');
await page.pdf({path: 'hn.pdf', format: 'A4', printBackground: true});
await browser.close();
})();
所以我需要帮助将循环变量传递给变量。有人曾遇到过这个问题吗?感谢您的帮助。
答案 0 :(得分:0)
编辑:
我测试了在puppeteer中抛出2个错误的页面:
无需等待jQuery和turn.js插件加载
评估失败:ReferenceError:未定义$
index
必须从1
开始,而不是0
或
错误:评估失败:TurnJsError
固定代码:
await page.waitForFunction('typeof(jQuery) == "function" && typeof($().turn) == "function"', {
timeout: 5000
});
for (let index = 1; index < 8; index++) {
await page.evaluateHandle((index) => {
$('.flipbook').turn('page', index);
}, index);
//await sleep(2000); // for debugging
}