0

I have x - pages of lists of 15 objects each. I would like to go through all the pages downloading the text of each of the 15 items in the list. The download works, but the same elements are written to the variable "a" (probably because the code executes faster than clicking from within the code). Is it possible to put it to sleep / wait for execution and then go to the next page?

var a = '';
for (let i = 0; i < 999; i++) {
    const paragraphs = document.querySelectorAll(".my-object-on-list");
    paragraphs.forEach(p => a = a + p.outerText);
    angular.element(document.getElementsByClassName('btn-forward')).click();
    setTimeout(1);//sleep for 1s?
}
user1617141
  • 115
  • 1
  • 11
  • in setTimeout it will take milliseconds. Try with 5000, it will give you 5 second window – Yatendrasinh Joddha Mar 02 '22 at 10:44
  • Changed to 5000, but still nothing - when I'm trying to view 'a' content shows before code loop though last page – user1617141 Mar 02 '22 at 10:47
  • 2
    `setTimeout` is not a sleep. It just delays some function to execute later. You can see more on "sleeping" here: [What is the JavaScript version of sleep()?](https://stackoverflow.com/q/951021) However, it's unlikely a random delay will solve your problem. You need to find *what* the problem is and wait for that. – VLAZ Mar 02 '22 at 10:49
  • Thanks @VLAZ, thats what I need, works like a charm – user1617141 Mar 02 '22 at 11:13

1 Answers1

0

There are many problems in your script

Try this

const paragraphs = document.querySelectorAll(".my-object-on-list");
cons ctn = 0;
const btn = document.getElementsByClassName('btn-forward')
let a = []
const run() {
  if (cnt >= paragraphs.length) {
    processResults();
    return 
  }
  a.push(p.outerText);
  angular.element(btn).click();
  cnt++
  setTimeout(run,3000);
}
run()
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Something wrong with function declaration, shouldn't it be const run = function () {...}? Sorry for the question, I'm relatively new to javascript. – user1617141 Mar 02 '22 at 11:00