I want to pass the for
loop variable to page.evaluate in puppeteer, in order to get the all gages of a gallery. But it doesn't work. It always reports UnhandledPromiseRejectionWarning: Error: Evaluation failed
.
The following is a example of my code:
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();
})();
So I need help to pass the loop variable to variable. Dose anyone met this problem before? Thanks for your kindly help.
Edit:
I tested the page it throw 2 errors in puppeteer:
Without waiting jQuery and turn.js plugin loaded
Evaluation failed: ReferenceError: $ is not defined
index
have to start from 1
not 0
or
Error: Evaluation failed: TurnJsError
Fixed code:
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
}