请帮助我达到预期的结果。 我将用文本填充页面上的每个输入字段:“123”。
let inputList = await page.$$('.form input');
inputList.map(async item => {
await item.type('123');
});
预期结果 - 每个字段 123。
最后一个输入字段的实际结果 - 112233。
最佳答案
一般来说,您正在尝试迭代数组并对数组中的每个项目执行异步操作。您可以通过多种方式实现这一点,for ... of
循环是一种方式。如果您不想循环而是想迭代,可以这样做:
await inputList.reduce(async (previousVal, currentVal) => {
await previousVal; // wait for the promise returned by the previous asynchronous call
return currentVal.type('123'); // call the next elementHandle in the array
}, Promise.resolve()); // the initial value that is passed to the callback function
关于javascript - Puppeteer.Iteration elementHandle 数组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48051709/