0
votes

I hope you can help me, I have been using node.js for a short time and only a week with puppeteer and I have made a small excrip to automate a task, when running the script it performs the process correctly at a glance.


However, the console throws me two types of errors:

1st - UnhandledPromiseRejectionWarning: TimeoutError: waiting for selector .flexMain> div> div> #PageContent_AuthorisedButtons> .btn failed: timeout 30000ms exceeded at new WaitTask

2nd - 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 ().


I would appreciate help to solve the problem since I have been stuck and do not see the solution.


const puppeteer = require('puppeteer');

(async () => {
    try {

        const browser = await puppeteer.launch({
            headless: false,
            userDataDir: "./cache",
            ignoreDefaultArgs: [
                "--disable-extensions",
                "--enable-automation"
            ],
            args: [
                "--no-sanbox",
                "--disable-setuid-sandbox",
                "--disable-dev-shm-usage",
                "--disable-gpu"
            ]
        })

        const page = await browser.newPage();
        page.setRequestInterception(true);
        page.on("request", async req => {
            if (req.url().endsWith(".png")) {
                req.abort();
            } else {
                req.continue();
                
                await page.waitForSelector('.flexMain > div > div > #PageContent_AuthorisedButtons > .btn');
                await page.click('.flexMain > div > div > #PageContent_AuthorisedButtons > .btn');
            }


        });



        page.on('error', e => console.log(e));

        await page.goto('http://example.com');

        await page.setViewport({ width: 1920, height: 926 });

        await page.waitForSelector('.flexMain > div > div > #PageContent_UnauthorisedButtons > .btn');
        await page.click('.flexMain > div > div > #PageContent_UnauthorisedButtons > .btn');

        await page.type('.row #SignInEmailInput', '[email protected]');

        await page.waitForSelector('.col-md-7 > #SignInForm > .form-group > div:nth-child(2) > a');
        await page.click('.col-md-7 > #SignInForm > .form-group > div:nth-child(2) > a');

        const collectCaptcha = await page.$('#adcopy-puzzle-image-image');
        collectCaptcha.screenshot({ path: 'src/img/capture-captcha.png' });

        const urlImageCaptcha = path.join(__dirname, 'img/capture-captcha1.png');

        // page.waitForTimeout(10000)


        // await browser.close()

    }
    catch (e) {

    }

})()
1

1 Answers

0
votes

Inside the page.on("request", ...) handler, you declare it as an async callback and you use await, yet you don't catch any exceptions from the await. Put a try/catch are the code in that callback.

   page.on("request", async req => {
        if (req.url().endsWith(".png")) {
            req.abort();
        } else {
            req.continue();

            // ==> you don't catch rejections from either of these await statments  <==
            await page.waitForSelector('.flexMain > div > div > #PageContent_AuthorisedButtons > .btn');
            await page.click('.flexMain > div > div > #PageContent_AuthorisedButtons > .btn');
        }
    });

You can catch these possible rejections with this:

   page.on("request", async req => {
      try {
        if (req.url().endsWith(".png")) {
            req.abort();
        } else {
            req.continue();

            await page.waitForSelector('.flexMain > div > div > #PageContent_AuthorisedButtons > .btn');
            await page.click('.flexMain > div > div > #PageContent_AuthorisedButtons > .btn');
        }
      } catch(e) {
           console.log(e);
           // do something here with the error
      }
    });

FYI, this code doesn't appear to actually do anything. you await a select and then a click, but then don't do anything after those occur.