简体   繁体   中英

How to use for loop inside an async function in node.js?

I am trying to login a webpage using a bunch of IDs.

I have created the code using single id and my code is running, but I am struck with how to introduce a loop inside the async function.

const puppeteer = require('puppeteer');

let scrape = async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('URL');
    const id=[IDs];

    for(const i of id)
    {
        await page.$eval('#username', el => el.value = i);
        await page.$eval('#password', el => el.value = 'PASSWORD');
        await page.click('#loginbutton');
        await page.waitFor(1000);
        const result = await page.evaluate(() => {
        let a=document.getElementById('signin-caption').innerText;
        let b=document.getElementById('statusmessage').innerText;
        if(b)
            return b;
        return a;
        });
        await console.log(result);
        if(result.startsWith('You'))
            break;
    }
    await browser.close();
    return result;
 };

scrape().then((value) => {
    console.log(value);
});

Getting the following error: "UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: i is not defined"

await page.$eval('#username', (el , i ) => el.value = i , i );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM