Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
Titiritero, esperando un selector y devolviendo datos desde dentro

Estoy cargando una página, interceptando sus solicitudes, y cuando aparece cierto elemento, dejo de cargar y extraigo los datos que necesito...

Aquí está el problema al que me enfrento.

Cuando se simplifica, el código se parece a esto:

 async function loadPage() { var contentLoaded = false; var content; //now i say when element shows up, do something // it is page.waitForSelector but for simplicity, i use a timeout // because the problem is the same //i set it to "show up" in 10 seconds here. //when it shows up, it sets the content to 100 (extracts the content i want) //and stores it.. setTimeout(()=>{ content = 100; contentLoaded = true; },10000) //Here i have a function that loads the page //Intercepts request and handles them //Until content is loaded page.on('request', req =>{ if(!contentLoaded) { // keep loading page } }) // this is the piece of code i would like to not run, // UNTIL i either get the data, or a timeout error // from page.waitForSelector... //but javascript will run it if it's not busy with the //loading function above... // In 10 seconds the content shows // and it's stored in DATA, but this piece of code has // already finished by the time that is done... // and it returns false... if(contentLoaded) {return content} else {return false} } var x = loadPage(); x.then(console.log); //should log the data or false if error occured

Gracias a todos por tomarse el tiempo de leer esto y ayudar, soy un novato, así que cualquier comentario o incluso material de lectura es bienvenido si creen que hay algo que no estoy entendiendo completamente.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Resuelto

Explicación sencilla:

Esto es lo que estaba tratando de lograr:

  1. Interceptar solicitudes de página para que pueda decidir qué no cargar y acelerar la carga
  2. Una vez que aparece un elemento en la página, quiero extraer algunos datos y devolverlos.

Estaba tratando de devolverlo así: (nota, todo el navegador y el manejo de errores se omitirán en estos, ya que solo desordenaría la explicación)

 var data = loadPage(url); async function loadPage(URL) { var data; page.waitForSelector( var x = //page.evaluate returns data to x... data = x; ) return data; }

Lo cual no funciona ya que return se ejecuta inmediatamente pero waitForSelector se ejecuta más tarde, por lo que siempre devolvemos undefined...

La forma correcta de hacerlo, o mejor dicho, la forma en que funciona para mí es devolver la promesa completa y luego extraer los datos...

 var data = loadPage(url); data.then(//do what needs to be done with the data); async function loadPage(URL) { var data = page.waitForSelector( var x = //page.evaluate returns data to x... data = x; ) return data; // we return data as a promise }

Espero que sea una explicación lo suficientemente sólida, si alguien necesita ver todo el asunto, podría editar la pregunta y colocar el código completo allí...

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!