如何在 puppeteer 中将 for 循环变量传递给 c
How can I pass the for loop variable to c in puppeteer
我想将 for
循环变量传递给 puppeteer 中的 page.evaluate,以获取画廊的所有规格。但它不起作用。它总是报告 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();
})();
所以我需要帮助将循环变量传递给变量。以前有人遇到过这个问题吗?感谢您的热心帮助。
编辑:
我测试了它在 puppeteer 中抛出 2 个错误的页面:
无需等待 jQuery 和 turn.js 插件已加载
Evaluation failed: ReferenceError: $ is not defined
index
必须从 1
开始,而不是 0
或
Error: Evaluation failed: 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
}
我想将 for
循环变量传递给 puppeteer 中的 page.evaluate,以获取画廊的所有规格。但它不起作用。它总是报告 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();
})();
所以我需要帮助将循环变量传递给变量。以前有人遇到过这个问题吗?感谢您的热心帮助。
编辑:
我测试了它在 puppeteer 中抛出 2 个错误的页面:
无需等待 jQuery 和 turn.js 插件已加载
Evaluation failed: ReferenceError: $ is not defined
index
必须从1
开始,而不是0
或Error: Evaluation failed: 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
}