skip to Main Content

I’m trying to automate a task using JavaScript by running it in the Google Chrome console (not sure if that matters, I’m pretty new). I have already done most of the script, but more problems keep popping up the moment I’m done with the "last bit" of the script. Essentially, I have a list of IDs to search up on a company webpage, numbering around 20k IDs, and can only search 20 IDs at a time. That portion is fine, but the problem is that I would need to wait for the webpage to finish loading, (which I think is synchronous) before moving on to the next 20 IDs, but my webpage would freeze using whatever methods I’ve tried. I would like to ask if an asynchronous solution is required if I would like to wait for the page to finish loading anyway?

What steps should I take to get to my solution? Thanks!

When my webpage loads, it would use an element with id ‘cover’ and change it’s display from ‘none’ to ‘block’. So far I’ve tried

  1. For Loop
    for (var s = 0; s < 1;) {
        tool20 = splitSearch(s); //tool20 gets the results of the 20 IDs
        for (var t = 0; t < tool20.length; t++){
            allTools.push(tool20[t]);
        }
        document.getElementById('bad').value = [];
        if(document.getElementById('cover').style.display === 'block'){
            inc = 0;
            }
        if(document.getElementById('cover').style.display === 'none'){
            inc = 1;  
            }
        s += inc;
}
  1. While loop
var i = 0;
search(); //function to search
//assuming the moment my script hits the search button the display is already on 'block'
While(i == 0){
    if(document.getElementById('cover').style.display === 'none'){
    inc = 1;
    //Remaining code to run after loading
    }
}

Both of these methods will end up freezing my page
(sorry for formatting I’m not sure how to use stack overflow)

3

Answers


  1. use addEventListener method

       window.addEventListener("load", (event) => {document.getElementById('cover').style.display = 'block';alert('page is fully loaded');});
    
    Login or Signup to reply.
  2. if you actually want to wait for the UI to load and do stuff on it, maybe see this answer, it explains why your page is freezing and how to wait for a property to change (in your case display change to ‘none’)
    Another option could be inspecting the API requests of the web page you are trying to automate by using the Network tab in Dev Tools, see how the requests are sent and simulate sending the requests yourself asynchronously using javascript, and then automate using javascript instead of the UI.

    Login or Signup to reply.
  3. if the cover display is block, then inc will always be 0. This creates an infinite loop. The same goes for the while loop.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search