Clicky

Puppeteer browser automation

Updated 1 week ago by Daniel

Puppeteer is a library for Node.js that provides an opportunity to automate processes with Chromium-based browser through high-level API over the Chrome DevTools Protocol. For example, you can create web crawlers that search and collect data by using the Mimic browser with masked fingerprints.

Multilogin port allocation

On 3.x versions of Multilogin, you need to predefine the application port in order to utilize Puppeteer automation. Here is how you can predefine Multilogin port in 3.x:

  1. Please go to C:\Users\%username%\.multiloginapp.com directory and open app.properies file
  2. Add the following string: multiloginapp.port=[PORT_NUMBER]
  3. Save the app.properties file

Thereafter, you will be able to refer to the Multiogin application through this port.

How to start

Step 1

Make sure that you have Node.js and npm package manager installed on your PC. Node.js and npm can be downloaded from the official Node.js website (the latest versions of Node.js include npm by default). Alternatively, you can use yarn for Node.js packages management.

You can check the version of your Node.js and npm by executing the following commands in terminal:

nodejs -v || node -v && npm -v
Step 2

Create a new npm project in current directory.

npm init -y
This command will create package.json file and -y parameter allows to skip the questionnaire and create a project with default settings.
Step 3

Install Puppeteer and request packages in the project directory.

npm install puppeteer@1.19.0 --save && npm install request --save
Please note that certain Puppeteer versions are only compatible with certain Chromium versions. Currently, (Multilogin 4.5) Mimic uses Chromium 77, which is compatible with Puppeteer version 1.19.0
Previous versions compatibility:

Chromium 73 (starting from Multilogin 4.3 and later)

Puppeteer 1.12.2

Chromium 72 (starting from Multilogin 4.0 and later)

Puppeteer 1.10.0

Chromium 67 (starting from Multilogin 3.0.1 and later)

Puppeteer 1.3.0

Step 4

Create .js file with your automation code. Please use the following code example for a reference:

const puppeteer = require('puppeteer');
const http = require('http');


async function startProfile(){
//Replace profileId value with existing browser profile ID.
let profileId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
let mlaPort = 35000;

/*Send GET request to start the browser profile by profileId.
Returns web socket as response which should be passed to puppeteer.connect*/
http.get(`http://127.0.0.1:${mlaPort}/api/v1/profile/start?automation=true&puppeteer=true&profileId=${profileId}`, (resp) => {
let data = '';
let ws = '';

//Receive response data by chunks
resp.on('data', (chunk) => {
data += chunk;
});

/*The whole response data has been received. Handling JSON Parse errors,
verifying if ws is an object and contains the 'value' parameter.*/
resp.on('end', () => {
let ws;
try {
ws = JSON.parse(data);
} catch(err) {
console.log(err);
}
if (typeof ws === 'object' && ws.hasOwnProperty('value')) {
console.log(`Browser websocket endpoint: ${ws.value}`);
run(ws.value);
}
});

}).on("error", (err) => {
console.log(err.message);
});
}

async function run(ws) {
try{
//Connecting Puppeteer with Mimic instance and performing simple automation.
const browser = await puppeteer.connect({browserWSEndpoint: ws});
const page = await browser.newPage();
await page.goto('https://multilogin.com');
await page.screenshot({ path: `/home/${process.env.USER}/Desktop/multiloginScreenshot.png` });
await browser.close();
} catch(err){
console.log(err.message);
}
}

startProfile();

Step 5

Launch the .js file through terminal in order to start your automation script.

nodejs example.js


Was this article helpful?