[英]How to use for loop inside an async function in node.js?
我正在尝试使用一堆ID登录网页。
我已经使用单个id创建了代码,并且我的代码正在运行,但是我对如何在async函数中引入循环感到惊讶。
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);
});
得到以下错误:“ UnhandledPromiseRejectionWarning:错误:评估失败:ReferenceError:我未定义”
await page.$eval('#username', (el , i ) => el.value = i , i );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.