0

I am scraping a list of users who liked a post on instagram. The following code runs perfectly fine:

await click(post);
await sleep(500);
var users = await findAll(user);
var userNames = users.map(async user => {
  var userNameText = await user.getText();
  return userNameText;
});
var result = await Promise.all(userNames);
console.log(result); // ['user1', 'user2'...]

But the modal with the likes only shows 10 users initially. To see the other users, you have to keep scrolling while it only loads a subset at a time. The following recursive function keeps scrolls down loading a single user incrementally for the number of users I want to get:

let likers = [];
await (async function theLoop(i) {
  await driver.sleep(400);
  findAll(user).then(async t => {
    let liker = await t[8].getText();
    await scroll(find(modal)); //Scroll inside the modal
    await likers.push(liker); //PUSH LIKER TO ARRAY
    console.log(liker);
    if (--i) {
      theLoop(i);
    }
  });
})(60);

The problem is that I am unable to get the list of all users from this function. If I do a console.log(likers) if fires immediately before the array is populated. same happens with any function I run after this recursive loop. Nothing I do works.

2
  • Is t an array of all users? (good variable names can be helpful). Why t[8]? Commented Apr 19, 2019 at 15:57
  • Because the new user only start appearing after several initial scrolls Commented Apr 19, 2019 at 16:11

1 Answer 1

2

Your problem is that your recursive function doesn't return anything, and you are awaiting neither findAll().then(…) nor the recursive call. Also you don't need recursion at all here, with async/await you can (should) write a plain loop:

for (var i=60; --i; ) {
  await driver.sleep(400);
  const t = await findAll(user); // don't use `then`
  let liker = await t[8].getText();
  await scroll(find(modal));
  likers.push(liker); // nothing to `await` here
  console.log(liker);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Weird, my first iteration of this code used a for loop but I had the same issue that the code after the loop wouldn't wait for the loop to finish, which ended up with me resorting to recursion. But this code you wrote just worked on the first try. Thanks!
However, just for the sake of learning, My recursive function DID return the liker initally, but this didn't work either

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.