Reputation: 57
I'm web scraping with puppeteer but when one element (image) is not found in one of the objects, I get an error UnhandledPromiseRejectionWarning: Error: Error: failed to find element matching selector ".css-13u5hxa.epu0oo22 img"
How to continue running the application even if the error occurs? I know that some objects that will be scraped won't have image but still would like to get them.
Below is the const that causes the error
const companyImage = await page.$eval(
// ".oc-photo-gallery .photo__10vsfGte img",
".css-13u5hxa.epu0oo22 img",
(img) => img.src
);
listings[i].companyImage = companyImage;
const listingModel = new GlassdoorDB(listings[i]);
await listingModel.save();
await sleep(1000); //1 second sleep```
Upvotes: 0
Views: 140
Reputation: 1502
Wrap it in a try/catch, log the error and continue
try {
const companyImage = await page.$eval(
// ".oc-photo-gallery .photo__10vsfGte img",
".css-13u5hxa.epu0oo22 img",
(img) => img.src
);
listings[i].companyImage = companyImage;
const listingModel = new GlassdoorDB(listings[i]);
await listingModel.save();
await sleep(1000); //1 second sleep```
} catch(err) {
console.log(err)
}
Upvotes: 1