Estoy tratando de hacer sorteos automáticos simplemente haciendo clic en un botón para participar cada vez que se reinicia el temporizador. No encuentro que funcione cómo puedo controlar varias páginas al mismo tiempo. Encontré "bringToFront()", como entendí, va desde la página 1 => 2 ... Usé esta función y otra para obtener el nombre de la página actual y no puedo obtener la función de la página.
(const this => "dar paso" = esperar browser.newPage();)
Si alguien me puede ayudar por favor gracias. Aquí el código principal que necesita:
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch({headless: false}); const login = await browser.newPage(); const anotherPage = await browser.newPage(); var currentPage = getActivePage(browser, 3000); var pageNum = 1; //Imagine idk is a giveaway website. await login.goto('example.com'); await anotherPage.goto('idk.com'); // if(loaded){ //const participateBtn = await (don't work) =>"currentPage" (can't find it with currentPage only with anotherPage) =>".$x" ("//button[@id='giveaway-enter']") const participateBtn = await currentPage.$x("//button[@id='giveaway-enter']") do{ if(pageNum <= 1){ await participateBtn[0].click(); console.log("Click! 1"); currentPage.bringToFront(); //Ya pas sa avec currentPage pageNum++; }else if(pageNum == 2){ await participateBtn[0].click(); console.log("Click! 2"); currentPage.bringToFront(); //Ya pas sa avec currentPage pageNum++; }else if(pageNum >= 3){ await participateBtn[0].click(); console.log("Click! 3"); pageNum = 1; } }while(loaded) } //Function i found. async function getActivePage(browser, timeout) { var start = new Date().getTime(); while(new Date().getTime() - start < timeout) { var pages = await browser.pages(); var arr = []; for (const p of pages) { if(await p.evaluate(() => { return document.visibilityState == 'visible' })) { arr.push(p); } } if(arr.length == 1) return arr[0]; } throw "Unable to get active page"; } })();Puede obtener la página predeterminada (primera pestaña) con:
const currentPage = browser.pages().then(allPages => allPages[0]);Reescribí su código y corrigí algunas funciones y la función bringToFront() que requieren esperar ya que esta función regresa como promesa.
PD: No sé si funcionará o no, ya que no conozco los sitios exactos que abrió este bot.
Y no olvides elegir esto como la respuesta correcta si esto te ayuda.
import puppeteer from 'puppeteer' ;(async () => { const browser = await puppeteer.launch({ headless: false }) const login = await browser.newPage() const anotherPage = await browser.newPage() const getActivePage = async (browser, timeout) => { const pages = await browser.pages() let activePage setTimeout(() => { for (let i = 0; i < pages.length; i++) { if (await pages[i].evaluate(() => document.visibilityState === 'visible')) { activePage = pages[i] } } throw 'Unable to get active page' }, timeout) return activePage } var currentPage = getActivePage(browser, 3000) var pageNum = 1 await login.goto('https://www.', {timeout: 0, waitUntil: 'networkidle0'}) await anotherPage.goto('idk.com', {timeout: 0, waitUntil: 'networkidle0'}) const participateBtn = await currentPage.$x('//button[@id="giveaway-enter"]') do { if (pageNum <= 1) { await participateBtn[0].click() console.log('Click! 1') await currentPage.bringToFront() pageNum++ } else if (pageNum == 2) { await participateBtn[0].click() console.log('Click! 2') await currentPage.bringToFront() pageNum++ } else if (pageNum >= 3) { await participateBtn[0].click() console.log('Click! 3') pageNum = 1 } } while (true) })()