UnhandledPromiseRejectionWarning:错误抛出到没有catch块的异步函数内部,promise无法通过.catch()处理

罗伯特·阿曼德

希望您能为我提供帮助,我使用puppeteer的时间很短,仅使用了一周,并且我做了一个简短的摘录来自动化任务,在运行脚本时,它一目了然地正确执行了该过程。


但是,控制台向我抛出两种类型的错误:

1st-UnhandledPromiseRejectionWarning:TimeoutError:等待选择器.flexMain> div> div> #PageContent_AuthorisedButtons> .btn失败:新的WaitTask超时超过30000ms

第二-UnhandledPromiseRejectionWarning:未处理的承诺被拒绝。该错误是由于在没有catch块的情况下抛出异步函数而引起的,或者是由于拒绝了未使用.catch()处理的诺言而引起的。


由于我被困住了,看不到解决方案,希望能帮助您解决问题。


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) {

    }

})()
jfriend00

page.on("request", ...)处理程序内部,您将其声明为async回调并使用await,但是您不会从await中捕获任何异常。try/catch在该回调中放置一个代码。

   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');
        }
    });

您可以通过以下方式捕获这些可能的拒绝:

   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
      }
    });

仅供参考,此代码似乎并未执行任何操作。您等待选择然后单击,但是在发生这些之后什么也不做。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章