tengo este codigo:
const puppeteer = require("puppeteer"); (async () => { const browser = await puppeteer.launch({ headless: false }); const page = await browser.newPage(); await page.goto("https://www.sisal.it/scommesse-matchpoint/quote/calcio/serie-a"); const [button1] = await page.$x('//div[@class="marketBar_changeMarketLabel__l0vzl"]/p'); button1.click(); const [button2] = await page.$x('//div[@class="listItem_container__2IdVR white marketList_listItemHeight__1aiAJ marketList_bgColorGrey__VdrVK"]/p[text()="1X2 ESITO FINALE"]'); button2.click(); })();El problema es que después de hacer clic en el botón 1, la página cambia y el titiritero ejecuta inmediatamente la siguiente línea de código, en cambio, quiero que espere a que se cargue la nueva página porque, de lo contrario, arrojará un error ya que no puede encontrar el botón 2.
Encontré esta solución en stackoverflow:
const puppeteer = require("puppeteer"); function delay(time) { return new Promise(function (resolve) { setTimeout(resolve, time); }); } (async () => { const browser = await puppeteer.launch({ headless: false }); const page = await browser.newPage(); await page.goto("https://www.sisal.it/scommesse-matchpoint/quote/calcio/serie-a"); const [button1] = await page.$x('//div[@class="marketBar_changeMarketLabel__l0vzl"]/p'); button1.click(); await delay(4000); const [button2] = await page.$x('//div[@class="listItem_container__2IdVR white marketList_listItemHeight__1aiAJ marketList_bgColorGrey__VdrVK"]/p[text()="1X2 ESITO FINALE"]'); button2.click(); })();Pero, por supuesto, esta no es la mejor solución.
Creo que tienes que modificar un poco en tu código:
await button1.click(); await page.waitForNavigation({waitUntil: 'networkidle2'});Para referencia, consulte la documentación .
Encontré una solución, aquí está el código:
const puppeteer = require("puppeteer"); (async () => { const browser = await puppeteer.launch({ headless: false }); const page = await browser.newPage(); await page.goto("https://www.sisal.it/scommesse matchpoint/quote/calcio/serie-a"); await page.waitForXPath('//div[@class="marketBar_changeMarketLabel__l0vzl"]/p'); const [button1] = await page.$x('//div[@class="marketBar_changeMarketLabel__l0vzl"]/p'); await button1.click(); await page.waitForXPath('//div[@class="listItem_container__2IdVR white marketList_listItemHeight__1aiAJ marketList_bgColorGrey__VdrVK"]/p[text()="1X2 ESITO FINALE"]'); const [button2] = await page.$x('//div[@class="listItem_container__2IdVR white marketList_listItemHeight__1aiAJ marketList_bgColorGrey__VdrVK"]/p[text()="1X2 ESITO FINALE"]'); button2.click(); })();