0

Help me please to achieve the expected result. I am going to fill each input field on the page with a text: '123'.

let inputList = await page.$$('.form input');
inputList.map(async item => {
   await item.type('123');
});

Expected Result - 123 in each field.

Actual Result - 112233 on the last input field.

page.$$(selector) API

4
  • 1
    You probably don't want the item.type calls to run in parallel, so don't use map but a for … of loop where await stops the iteration. Commented Jan 1, 2018 at 17:25
  • @Bergi Thanks! for ... of works for my case :-) Commented Jan 1, 2018 at 17:35
  • Looks like you shouldn't have been using map anyway as you didn't want to produce a new array. Commented Jan 1, 2018 at 17:50
  • @Bergi OK! Thank you for your guidance. Commented Jan 1, 2018 at 18:23

1 Answer 1

1

In a general sense, you are trying to iterate over an array and perform an asynchronous action on each item in the array. You can accomplish that several ways, the for ... of loop is one way. If you don't want to loop but instead want to iterate you can do it this way:

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.