I've been writing a simple program that opens several pages and performs some simple scraping, and login functions. I have already managed to open new pages on new tabs once I run the browser, but when the browser is already open I am unable to open more tabs in that same browser, and have to open them in a new different browser window.
Is there any workaround this? Or is there any way to get puppeteer to open new pages in the same browser window if a browser window is already open? Thanks in advance.
index.js (main application file)
async function runBrowser(pageID) {
const browser = await puppeteer.launch({
"headless": false,
"defaultViewport": null,
"args": ['--start-maximized'],
});
//CHANGE THE PAGE THAT OPENS
switch(pageID){
case 0:
login.openCursosPuntoD(browser);
break;
case 1:
facebook.openFacebookPuntoD(browser);
break;
case 2:
instagram.openInstagramPuntoD(browser);
break;
case 3:
twitter.openTwitterPuntoD(browser);
break;
case 4:
youTube.openYouTubePuntoD(browser);
break;
case 5:
webPage.openPaginaWebPuntoD(browser);
break;
default:
break;
}
};
runBrowser(0);
instagramPuntoD.js (an example of an open function file)
function openInstagramPuntoD(browser){
actions.runPage('https://www.instagram.com/xxxx/',false,browser);
};
module.exports = {
openInstagramPuntoD,
};
actions.js (contains functions of actions that the scraper can perform)
async function runPage(url,doLogin,browser){
const page = [];
const pageNumber = (await browser.pages()).length+1;
page[pageNumber] = await browser.newPage();
await page[pageNumber].goto(url);
if(doLogin == true){
var user = getUsername();
var pass = getPassword();
login(user,pass,page[pageNumber]);
}
else{}
}
*NOTE The irrelevant parts of the code were excluded.
Ok so after a few research,
You have to use a userDataDir to reuse the cache, and make browser reuse the current active window. Otherwise it's just start from a traditional 'open a new window'
puppeteer.launch({
userDataDir: 'PATH TO DATA FOLDER',
})
You can find the Data Folder with chrome://version
(Profile Path), but don't forget to erase the /Default
as the end of the path
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With