Home > Back-end >  How i get active browser page with puppeteer?
How i get active browser page with puppeteer?

Time:02-14

im trying to do a auto giveaways just by clicking on a button for participate everytime the timer reset. I don't find working how i can control multiple page at the same time. i found "bringToFront()" as I understood goes from page 1 => 2 ... I used this function and another one for get current page name and i can't get page function.

(const this => "giveway" = await browser.newPage();)

If someone can help me please thanks. Here the main code you need:

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

CodePudding user response:

You can get the default page (1st tab) with:

const currentPage = browser.pages().then(allPages => allPages[0]);

CodePudding user response:

I've rewritten your code and fixes some functions and the bringToFront() function which require await since this function return as promise.

PS: I don't know if it's gonna work or not at all since i don't know the exact sites which opened by this bot.
And don't forget to choose this as the right answer if this helps you.

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)
})()
  • Related