Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
not iterate until condition is met

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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

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()));
}

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda