1

My mind is about to break trying to figure out this.

I'm trying to automate adding some machines to a list from the google chrome console, since the website is not mine and i do not have access to the code.

enter image description here

I have a big list of machine that i need to add to the right column, i can't import it so that's why i'm trying to automate the process.

This is my code:

// input box
var search = document.getElementById(
  "CustomControl_ml_ms_m_si_m_tic_pc_xAssignedResources_m_computerSelector_WrappedSelector_ml_ms_m_si_m_tic_pc_xAssignedResources_m_computerSelector_WrappedSelector_mainGrid_ctl00_SearchBox"
);
// add to list btn
const addToList = document.querySelector(".scbMiddleCell input:nth-child(2)");
// first item on the left column
const primeraCelda = document.querySelector(".scgc");

// change the value of the input box
search.value = "XXX-MXXXX";

// create a keyup event
var ev = document.createEvent("Event");
ev.initEvent("keyup");
ev.which = ev.keyCode = 13;
// run the event, this way the ajax function kicks in and i'm left with only one item in the left column
search.dispatchEvent(ev);

setTimeout(() => {
// simulate the left item row selection
  primeraCelda.click();
}, 1000);
setTimeout(() => {
// click on the button to add the item to the right column
  addToList.click();
}, 1500);

Now this works only when i'm using a single value, if i create and array with values, and a foreach loop, it directly iterates through all the items in the array and only the last item gets added to the right column.

I can't figure out how to NOT iterate until the item is added to the right column.

Simo
  • 57
  • 8

1 Answers1

0

It took me a whole lot of searching, but i was finally able to find a solution using promises, promisfiying setTimeout.

I found the solution on this thread: JavaScript ES6 promise for loop

const delay = ms => new Promise(resolve => setTimeout(resolve, ms));


for (let i = 0, p = Promise.resolve(); i < myArray.length; i++) {
  const element = myArray[i];
  const celdaClick = () => primeraCelda.click();
  const addtoListClick = () => addToList.click();

  p = p
    .then(() => delay(1000, (search.value = element)))
    .then(() => delay(1000, search.dispatchEvent(ev)))
    .then(() => console.log(i))
    .then(() => delay(1500, celdaClick()))
    .then(() => delay(1500, addtoListClick()));
}

Simo
  • 57
  • 8