Tengo un código que usa Puppeteer que estoy usando para obtener palabras aleatorias de un sitio web que las genera. El proceso simula un clic desplegable, una entrada y un clic del mouse y luego toma el texto generado en la página siguiente y lo guarda en un archivo .json en el cwd.
Estoy tratando de encontrar la mejor manera de mostrar algún tipo de descriptor de progreso de los acontecimientos como un porcentaje (ya que me gustaría que el código Titiritero se ejecutara en modo sin cabeza) pero no puedo entender cómo podría hacer que funcione bajo mi configuración actual. ¿Alguien tiene alguna sugerencia? Además, este es mi primer trimestre en JS, por lo que entiendo que es posible que haya hecho cosas que no están pulidas, por favor sea amable.
// instantiate puppeteer const puppeteer = require('puppeteer'); // function call for puppeteer async function launchSearch(){ // website that generates random words where the words will come from const url = 'https://www.sodacoffee.com/words/list-generator'; // button div id to click and generate more word searches const buttonClick = '#ctl00_ContentPane_btn'; // div id for the drop-down selector on the url asking how many results we want const numResultsPerClick = '#ctl00_ContentPane_resultscounter'; // puppeteer browser launch options const browser = await puppeteer.launch({ // headless == no graphical representation of the browser headless: false, }); // create new browser element named page const page = await browser.newPage(); // go to word generator URL await page.goto(url); // variable searches DOM for div id (declared above) const numWordsScrape = await page.$(numResultsPerClick); // select the element await numWordsScrape.click() // type 50 to return 50 random words await numWordsScrape.type('50'); // to prevent some errors due to promises, Promise.all() seemed to be best to get results await Promise.all([ // wait until the page has loaded (url) page.waitForNavigation(), // click button page.click(buttonClick) ]) // site *should* have advanced forward to the next page with 50 results const textAfterButtonClick = await page.evaluate( // create an array from the results of the query to the DOM, and map those specific elements <tr> -> to their inner text values () => Array.from(document.querySelectorAll('#ctl00_ContentPane_GridView1 tbody tr') ).map((elem) => elem.innerText.trim()) ); // instantiate file handling const fs = require('fs'); const file = 'word.txt'; fs.writeFileSync('./words.json', JSON.stringify(textAfterButtonClick), err => err ? console.log(err): null); // close instance of browser await browser.close(); } launchSearch();