0
votes

I get a UnhandledPromiseRejectionWarning: Unhandled promise rejection while doing simple web scraping with puppeteer, I have used the exact same code in another projects and it worked, I dont know why it aint working now. Full eror next line:

node .\scrapers.js
(node:9748) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'getProperty' of undefined
    at scrapeChannel (C:\Users\João Teixeira\OneDrive\code\learning\js\webscrapingapp\server\scrapers.js:10:27)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

(node:9748) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

(node:9748) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated.

In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Here's the code:

const puppeteer = require('puppeteer')

async function scrapeChannel(url) {

    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto(url);

    const [el] = await page.$x('//*[@id="text"]');
    const text = await el.getProperty('textContent');
    const name = await text.jsonValue();

    const [el2] = await page.$x('//*[@id="img"]');
    const src = await el2.getProperty('src');
    const avatarURL = await src.jsonValue();
    console.log({name, avatarURL})
    browser.close();

    return {name, avatarURL}
}
scrapeChannel('https://www.youtube.com/user/Microsoft');
1
use try/catch in async/await - Jaromanda X
I did as you said and now it just logs this: TypeError: Cannot read property 'getProperty' of undefined at scrapeChannel (C:\Users\João Teixeira\OneDrive\code\learning\js\webscrapingapp\server\scrapers.js:10:31) at processTicksAndRejections (internal/process/task_queues.js:97:5) But in the puppeteer docs its like it exists - Tyxe
either el or el2 are undefined - so ... - Jaromanda X

1 Answers

0
votes

Your call to page.$x (after being awaited) is not returning one iterable with at least one element OR the first element of the returned iterable is undefined. Thus, el or el2 (depending on the line they are) is being assigned with undefined. Since undefined does not have a property getProperty, it throws. You are not catching that error, hence the notice.

As @Jaromanda X said, use try/catch inside the async function. Or, since async functions return a Promise, use scrapeChannel('https://www.youtube.com/user/Microsoft').catch( /**/ ); to catch any error throw inside that function, avoiding the Node.js UnhandledPromiseRejectionWarning.