puppeteer - 意外令牌新(Promise),无法嵌套try / catch

时间:2017-10-23 03:05:28

标签: node.js headless puppeteer

所以我试图嵌套我的try / catch以使事情更快更顺畅。

这是外面的原始块(这是有效的):

    try {
        await page.waitFor("#login-form-os-captcha", { timeout: 1500 });
        security = true
        captcha = await new Promise( function (resolve, reject) {
            prompt.get(['captcha'], function (err, result) {
                console.log('Command-line input received:');
                console.log('  captcha: ' + result.captcha);
                if (result)
                {
                    resolve(result.captcha);
                }
                else
                {
                    reject("error with prompt captcha");
                }
            })
        });
    }
    catch (e) {
        security = false
    }

这是我尝试嵌套这种逻辑:

    try
    {
        if (form)
        {
            await page.evaluate( (result) => {
                document.querySelector("#login-form-username").value = 'user'
                document.querySelector("#login-form-password").value = result.password // require password
                try
                {
                    let captcha_input = document.querySelector("#login-form-os-captcha")
                    if (captcha_input)
                    {
                        /* error here, Unexpected token new */
                        captch_input.value = await new Promise( function (resolve, reject) {
                            prompt.get(['captcha'], function (err, result) {
                                console.log('Command-line input received:');
                                console.log('  captcha: ' + result.captcha);
                                if (result)
                                {
                                    resolve(result.captcha);
                                }
                                else
                                {
                                    reject("error with prompt captcha");
                                }
                            })
                        });
                    }
                }
                catch (e)
                {
                    console.log("captcha input error")
                    console.log(e);
                }

                document.querySelector("#login-form-submit").click()
            }, result)
        }
    }
    catch (e)
    {
        console.log("evaluate fail");
        console.log(e);
        process.exit(1);
    }

1 个答案:

答案 0 :(得分:0)

目前这样做,一个接一个。仍然不是我最想要的东西。

    try
    {
        await page.waitFor("#login-form-username", { timeout: 3000 })
    }
    catch (e)
    {
        console.log("No page or waited too long")
        process.exit(1);
    }

    // Check for captcha
    try
    {
        await page.waitFor("#login-form-os-captcha", { timeout: 500 })
        security = true
    }
    catch (e)
    {
        console.log("No captcha")
    }

    // Input captcha if there was a captcha
    if (security)
    {
        try {
            let captcha =
                await new Promise( function (resolve, reject) {
                    prompt.get(['captcha'], function (err, result) {
                        console.log('Command-line input received:');
                        console.log('  captcha: ' + result.captcha);
                        if (result)
                        {
                            resolve(result.captcha);
                        }
                        else
                        {
                            reject("error with prompt captcha");
                        }
                    })
                });
            console.log("captcha", captcha)
            await page.evaluate( (captcha) => {
                document.querySelector("#login-form-os-captcha").value = captcha
            }, captcha);
        } catch (e) {
            console.log("captcha error")
        }
    }

更新

好吧事实证明事先缺少async