Puppeteer automation example
Puppeteer is a powerful Node.js library developed by Google that provides a high-level API to control Chromium browsers via the DevTools Protocol. When paired with Indigo, Puppeteer enables seamless automation of Mimic browser profiles, allowing you to perform tasks like web scraping, testing, or automated browsing with customized browser fingerprints.
This guide walks you through setting up Puppeteer to automate Mimic profiles, including prerequisites, installation steps, and a complete script example. By the end, you'll have a working automation setup to interact with any website using Indigo's Mimic profiles.
Prerequisites
Before diving into the setup, ensure you have the following:
- Node.js and npm: Download and install Node.js from the official website.
- Indigo Account: A valid Indigo X account with access to Mimic browser profiles.
- Folder and Profile IDs: Obtain these from Indigo using DevTools or Postman.
- Agent Connection: Ensure the Indigo agent is running to enable profile launching.
- Puppeteer Compatibility: Verify that your Puppeteer version is compatible with the current Mimic core version (check Indigo release notes).
Mimic profiles (Chromium-based) are supported by Puppeteer. Stealthfox profiles (Firefox-based) are not compatible.
Setting Up Your Environment
Follow these steps to prepare your development environment:
1. Verify Node.js Installation
Ensure Node.js and npm are installed by running:
node -v
npm -v
If not installed, download and install Node.js from nodejs.org.
2. Create a Project Directory
Set up a new Node.js project:
mkdir puppeteer-indigo
cd puppeteer-indigo
npm init -y
This creates a package.json
file in your project directory.
3. Install Dependencies
Install Puppeteer, Axios, and MD5 libraries:
npm install puppeteer axios md5
- Puppeteer: Controls the browser.
- Axios: Handles HTTP requests to Indigo's API.
- MD5: Used for password hashing (optional, depending on your setup).
Writing the Automation Script
Below is a complete script to authenticate with Indigo, launch a Mimic profile, and automate a simple task (navigating to a website and taking a screenshot). The script is modular and includes error handling.
Script Overview
The script performs the following steps:
- Authenticates with Indigo's API to obtain a token.
- Launches a Mimic profile using Puppeteer.
- Opens a webpage and captures a screenshot.
Full Script
const puppeteer = require('puppeteer');
const md5 = require('md5');
const axios = require('axios');
const HEADERS = {
'Content-Type': 'application/json',
'Accept': 'application/json',
};
// Replace with your Indigo X account details
const acc_info = {
email: '[email protected]',
password: md5('your-password'), // MD5 encryption optional
};
// Replace with your Folder ID and Profile ID
const folder_id = 'your-folder-id';
const profile_id = 'your-profile-id';
async function get_token() {
const signIn_URL = 'https://api.indigobrowser.com/user/signin';
try {
const response = await axios.post(signIn_URL, acc_info, { headers: HEADERS });
return response.data.data.token;
} catch (error) {
console.error('Error fetching token:', error.message);
console.error('Response data:', error.response?.data);
return false;
}
}
async function start_browserProfile() {
const token = await get_token();
if (!token) {
console.error('Failed to obtain token. Exiting...');
return;
}
// Update headers with the bearer token
HEADERS.Authorization = `Bearer ${token}`;
// Launch Mimic profile
const profileLaunch_URL = `https://launcher.indigobrowser.com:45001/api/v2/profile/f/${folder_id}/p/${profile_id}/start?automation_type=puppeteer&headless_mode=false`;
try {
const response = await axios.get(profileLaunch_URL, { headers: HEADERS });
const browserURL = `http://127.0.0.1:${response.data.data.port}`;
// Connect Puppeteer to the browser
const browser = await puppeteer.connect({
browserURL,
timeout: 10000,
});
// Open a new page and navigate
const page = await browser.newPage();
await page.goto('https://indigobrowser.com/'); // Replace with your target URL
await page.screenshot({ path: 'example.png' });
// Clean up
await page.close();
await browser.close();
} catch (error) {
console.error('Error launching profile:', error.message);
console.error('Response data:', error.response?.data);
}
}
start_browserProfile();
Configuration Steps
-
Insert Account Details:
- Replace
[email protected]
andyour-password
with your Indigo X account credentials. - Note: MD5 encryption for the password is optional unless required by your setup.
- Replace
-
Add Folder and Profile IDs:
-
Customize the Target URL:
- Replace
https://indigobrowser.com/
with the website you want to automate.
- Replace
Running the Script
To execute the automation:
- Start the Indigo Agent: Ensure the agent is running to allow profile launching.
- Check Compatibility: Confirm that your Puppeteer version aligns with the Mimic core version (see release notes).
- Run the Script:
node script.js
The script will:
- Authenticate with Indigo.
- Launch the specified Mimic profile.
- Navigate to the target URL and save a screenshot as
example.png
.
If you encounter connection issues, verify the Indigo agent is active and the folder_id
and profile_id
are correct.