JavaScript Does Not Wait For Window to Load

53 views Asked by At

I am writing a code meant for a browser's console without libraries that types some textA from an array in a searchBar1, hits the searchButton1, waits for the window to load, types some textB in a searchBar2, hits the searchButton2, clicks a circle, then clicks a final button. When I run each of the lines of the code separately, manually waiting for each command to be processed, the code runs as it should. However, whenever I try to run the code all at once, all code placed after clicking the searchButton1 does not work.

The issue isn't that Preserve Logs isn't turned on - it is. I believe the problem is that JS simply isn't waiting for the window to load

I've tried using the load and DOMContentLoaded events on the window and document properties, tried using onload and await, and no luck in any case. I even tried using chrome.webNavigation.onCompleted.addListener, but, curiously enough, it didn't even run on the console. Here's the code below for reference:

//Declare CPFs Array
let cpfs = \\ List goes here 

// Declare text variable containing the msg you want the computer to send
let text = "visa"

//Declare search bar variable
let searchBar = document.querySelector("input.form-control:nth-child(1)")

//Declare search button variable
let searchButton = document.querySelector(".btn-primary")

//loops through CPFs Array
for (var i = 0; i < cpfs.length; i++) {

    //Types the client's name
    searchBar.value = cpfs[i]
    searchBar.dispatchEvent(
        new Event("input", { bubbles: true, cancelable: true })
    );

    //Clicks the search button
    searchButton.click()
    
    //Wait for window to load
    window.addEventListener('load', function () {
        //assign variable for the second search bar
        var inputBar = document.querySelector("input.form-control:nth-child(2)")

        //Sets value on the second search bar to the text you are looking for in the message to be sent 
        inputBar.value = text
        inputBar.dispatchEvent(
            new Event("input", { bubbles: true, cancelable: true })
        );

        //assign variable for the circle
        var circle = document.querySelector("html body.skin-blue.pg-login.sidebar-mini div#app.app.ng-scope div.wrapper.ng-scope div.content-wrapper section.content div.ng-scope div.row.ng-scope div.box.box-warning div.box-header div.col-md-12 div.box.box-default.box-solid div.box-body div.table-responsive div#envioNotificacoesTable_wrapper.dataTables_wrapper.form-inline.dt-bootstrap.no-footer div.row div.col-sm-12 table#envioNotificacoesTable.table.table-striped.envioNotificacoes.ng-isolate-scope.no-footer.dataTable tbody.ng-scope tr.ng-scope.odd td input.left-10")

        //click the circle
        circle.click()

        //Assigns variable for the button to send the message
        var sendButton = document.querySelector(".btn-success")

        //Sends the message
        sendButton.click()
    })

    //Wait for message to be sent
    window.addEventListener('load', function () {
    alert("Message sent!")  

    })
}

All help is welcome, and thanks in advance!

0

There are 0 answers