I wrote a script to take screenshots of 50 size charts on a webpage. Each chart is contained in a element. The funny thing is, only the first three table charts are captured, the rest of the PNG files are blank, completely white.
Since the charts are pulled from a database, I thought it could be that the page hadn't finished loading before the screenshots are taken, so I added { "waitUntil": "networkidle0" } but that didn't solve anything. Still, the script only creates screenshots of the first three charts, 0.png, 1.png and 2.png. The rest of the PNG files, 3.png - 49.png, are created but just white data.
What could be the issue? If I visit the page on my browser, all 50 charts load perfectly, so why is Puppeteer making a screenshot of only the first three? Here is my script:
const puppeteer = require( 'puppeteer' );
( async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto( 'http://www.example.com/size-charts.php', { "waitUntil": "networkidle0" } );
// Get a list of all elements.
const elements = await page.$$( 'div.chartContainer' );
for( let i = 0; i < elements.length; i++ ) {
try {
// get screenshot of a particular element
await elements[ i ].screenshot( { path: `${ i }.png` } );
}
catch( e ) {
console.log( `Couldn't take a screenshot of the element with the index of: ${ i }. Reason: `, e );
}
}
await browser.close();
} )();