0

I want to remove some connection from a Social Media account using JavaScript. If I execute the statements line by line, it works perfectly, but when I execute them in a block of code, it doesn't work.

THE BLOCK CODE

function sleep(delay) {
    var start = new Date().getTime();
    while (new Date().getTime() < start + delay);
}


for(var i=0;i<5;i++) {
    //Click on connection
    sleep(1000);
    document.getElementsByClassName('reusable-search__entity-result-list list-style-none')[0].getElementsByClassName('entity-result__title-text t-16')[1].getElementsByClassName('app-aware-link')[0].click();
    sleep(1000);
    //Remove connection button
    document.getElementsByClassName('ivm-view-attr__img--centered EntityPhoto-circle-3 lazy-image ember-view')[1].click();
    sleep(1000);
    document.getElementsByClassName('artdeco-dropdown__content-inner')[0].getElementsByTagName('li')[6].getElementsByClassName('pvs-profile-actions__action display-flex align-items-center artdeco-dropdown__item artdeco-dropdown__item--is-dropdown ember-view')[0].click();
    //Back
    sleep(1000);
    history.back();
}

I get every time the following error: TypeError: undefined is not an object (evaluating 'document.getElementsByClassName('artdeco-dropdown__content-inner')[0].getElementsByTagName')

It seems that after Click on connection part, the page is not loading, and it does only after the block is done. (I tried also without for loop, only those statements, but it happened the same like in the loop).

jefi
  • 185
  • 1
  • 13
Paul Viorel
  • 234
  • 1
  • 11
  • 3
    This sleep implementation is poor, since it will block the code and any interaction on the page while it runs. – evolutionxbox Jan 06 '22 at 13:12
  • 3
    You should use `setTimeout` – gkucmierz Jan 06 '22 at 13:14
  • *"It seems that after Click on connection part, the page is not loading"* - Because the thread is blocked by your loop. Refer to the linked duplicate for how to "sleep" in JavaScript while still allowing the page to load/render/etc. – David Jan 06 '22 at 13:16

0 Answers0