I've been trying to get puppeteer to launch a unique instance for every profile stored in a .json file. This is because currently I am stuck creating a new folder with all my code and a unique .json file for every account/instance I want to run. I'd prefer if I could just store all my info in 1 .json file and then have my code launch a unique instance for each profile.
Goal:
- Input all profile information in .json file
- Have code launch a unique instance for every profile in the list
- Every unique instance should only be using the profile code
Example: Puppeter instance 1 launch with profile 1, puppeteer instance 2 launch with profile 2, etc.
Example of settings.json
[
{
"email": "email1@gmail.com"
},
{
"email": "email2@gmail.com"
},
{
"email": "email3@gmail.com"
}
]
Example of main.js
const fs = require('fs');
const puppeteer = require('puppeteer');
const profile = JSON.parse(fs.readFileSync('./settings.json'));
var id = 0
while (id <= 2) {
emailInfo = profile[id].email;
console.log(emailInfo)
botRun()
id++;
}
function botRun() {
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.waitForTimeout(500)
console.log('function ' + emailInfo) //pretend this is page.type --> it would result in 'email3@gmail.com' for all instances since this is what the var is now but I want it to stay with the info in the loop
await browser.close();
})();
}
Obviously this is horrendously wrong since emailInfo var will update therefore resulting in puppeteer applying the latest value. Is there any way I can make each puppeteer instance stick with the unique data?
Edit 1:
Managed to get the workaround but now I seem to have ran into a new issue. Basically, in one point of my script I tell the browser to close the tab and reopen a new one. It closes each tab in each individual browser fine but when I use "await browser.newPage();" it sends all the new tabs to just 1 browser instead of staying in their respective browser.
const puppeteer = require('puppeteer-extra');
const fs = require('fs');
const botRun = async emailInfo => {
browser = await puppeteer.launch({
args: [],
headless: false,
ignoreHTTPSErrors: true,
slowMo: 5,
});
const page = await browser.newPage();
await page.waitForTimeout(2500)
// do stuff with emailInfo
await page.close(); // works fine - will close tab for each browser
await browser.newPage(); // suddenly sends all tabs to 1 browser
};
(async () => {
const profile = JSON.parse(fs.readFileSync("./settings.json"));
await Promise.all(profile.map(({email}) => botRun(email)));
})();
Here is an image for clarification. My goal is to keep the tabs in their respective browser rather than suddenly all being thrown to 1 browser: