Reputation: 109
In my scraping code, the second console print never gets executed, I'm unsure of what I'm doing wrong here, I'm new to the async and promises in javascript. ( the download picture function works )
async function scrape() {
// ...
await page.goto(URL, { waitUntil: "networkidle2" });
let picturetest = true;
if (picturetest) {
console.log("this gets printed");
await downloadPictures(page);
console.log("this never gets printer");
}
}
// await browser.close();
scrape();
async function downloadPictures(page) {
const elements = await page.$$(
"body > table > tbody > tr:nth-child(3) > td > form > table > tbody > tr:nth-child(2) > td:nth-child(2) img"
);
for (let i = 0; i < elements.length; i++) {
// get screenshot of a particular element
await elements[i].screenshot({ path: `${i}.png`, quality: 100 });
}
}
Upvotes: 0
Views: 99
Reputation: 1792
There are two possibilities:
downloadPictures
function is caught in an infinite loopGiven that your loop looks fairly straightforward, you should focus on #2 first. Start by adding a top-level error handler, where you currently invoke scrape()
:
scrape().catch((error) => { console.error(error); });
This may or may not reveal the problem. You can narrow in on it by adding try/catch blocks around the individual await
lines:
try {
await someAsyncCall();
} catch (error) {
console.error('someAsyncCall failed', error);
}
Upvotes: 1