Playwright automation example
Playwright is a powerful open-source library for automating web browsers, enabling tasks like navigation, form filling, and data extraction. When combined with Indigo X, it allows you to manage and automate Mimic browser profiles for advanced web scraping and testing workflows.
In this guide, we'll walk through setting up Playwright to automate a Mimic browser profile using both JavaScript and Python. You'll learn how to authenticate with Indigo X, launch a browser profile, and perform basic automation tasks.
Playwright for Stealthfox is not yet available in Indigo X. Ensure compatibility between Playwright and the Mimic core version by checking the release notes.
Prerequisites
Before you begin, ensure you have the following:
JavaScript
- Node.js: Download and install from the official website.
- Verify installation:
node -v
npm -v - A Indigo X account with your
email
,password
,folder_id
, andprofile_id
(find these using DevTools or Postman). - Project setup:
mkdir playwright-indigo
cd playwright-indigo
npm init -y
npm install playwright axios md5
Python
- Python 3.x: Ensure it's installed.
- Required libraries:
pip install requests playwright
playwright install - A Indigo X account with your
USERNAME
,PASSWORD
,FOLDER_ID
, andPROFILE_ID
.
Ensure the Indigo X agent is connected to enable profile launching.
Automation Workflow
The automation process involves:
- Authenticating with Indigo X to obtain a token.
- Launching a Mimic browser profile via the Indigo X API.
- Connecting Playwright to the browser profile.
- Performing actions like navigating to a webpage and taking a screenshot.
Below are example scripts for both JavaScript and Python.
Example Scripts
- JavaScript
- Python
JavaScript Example
This script authenticates with Indigo X, launches a Mimic browser profile, and automates a simple task using Playwright.
const { chromium } = require('playwright');
const md5 = require('md5');
const axios = require('axios');
const HEADERS = {
"Content-Type": "application/json",
"Accept": "application/json",
};
const acc_info = {
email: "YOUR_EMAIL", // Replace with your Indigo X email
password: md5("YOUR_PASSWORD"), // Replace with your password
};
const folder_id = "YOUR_FOLDER_ID"; // Replace with your folder ID
const profile_id = "YOUR_PROFILE_ID"; // Replace with 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:", error.message);
console.error("Response data:", error.response?.data);
return false;
}
}
async function start_browserProfile() {
const token = await get_token();
if (!token) return;
HEADERS.Authorization = `Bearer ${token}`;
const profileLaunch_URL = `https://launcher.indigobrowser.com:45011/api/v2/profile/f/${folder_id}/p/${profile_id}/start?automation_type=playwright&headless_mode=false`;
try {
const response = await axios.get(profileLaunch_URL, { headers: HEADERS });
const browserURL = `http://127.0.0.1:${response.data.data.port}`;
const browser = await chromium.connectOverCDP(browserURL, { timeout: 10000 });
const context = browser.contexts()[0];
const page = await context.newPage();
await page.goto("https://indigobrowser.com/");
await page.screenshot({ path: "example.png" });
await page.close();
} catch (error) {
console.error("Error:", error.message);
console.error("Response data:", error.response?.data);
}
}
start_browserProfile();
Running the Script
- Save the script as
index.js
. - Ensure the Indigo X agent is running.
- Run the script:
node index.js
Python Example
This script performs the same tasks as the JavaScript example but uses Python.
import hashlib
import requests
import time
from playwright.sync_api import sync_playwright
INDIGO_BASE = "https://api.indigobrowser.com"
HEADERS = {"Accept": "application/json", "Content-Type": "application/json"}
USERNAME = "YOUR_EMAIL" # Replace with your Indigo X email
PASSWORD = "YOUR_PASSWORD" # Replace with your password
FOLDER_ID = "YOUR_FOLDER_ID" # Replace with your folder ID
PROFILE_ID = "YOUR_PROFILE_ID" # Replace with your profile ID
def sign_in() -> str:
payload = {
"email": USERNAME,
"password": hashlib.md5(PASSWORD.encode()).hexdigest(),
}
r = requests.post(f"{INDIGO_BASE}/user/signin", json=payload)
if r.status_code != 200:
print(f"Error during login: {r.text}")
return None
return r.json()["data"]["token"]
HEADERS["Authorization"] = f"Bearer {sign_in()}"
def start_profile():
with sync_playwright() as pw:
resp = requests.get(
f"https://launcher.indigobrowser.com:45011/api/v2/profile/f/{FOLDER_ID}/p/{PROFILE_ID}/start?automation_type=playwright&headless_mode=false",
headers=HEADERS)
if resp.status_code != 200:
print(f"Error while starting profile: {resp.text}")
return
print(f"Profile {PROFILE_ID} started.")
browserPort = resp.json()["data"]["port"]
browserURL = f"http://127.0.0.1:{browserPort}"
browser = pw.chromium.connect_over_cdp(endpoint_url=browserURL)
context = browser.contexts[0]
page = context.new_page()
page.goto('https://indigobrowser.com/')
time.sleep(5)
page.screenshot(path='example.png')
page.close()
start_profile()
Running the Script
- Save the script as
main.py
. - Ensure the Indigo X agent is running.
- Run the script:
python main.py
Configuration Details
Variable | Description | How to Find |
---|---|---|
email / USERNAME | Your Indigo X account email | Your account settings |
password / PASSWORD | Your Indigo X account password (MD5 hashed for API) | Your account settings |
folder_id / FOLDER_ID | ID of the folder containing the browser profile | Use DevTools or Postman |
profile_id / PROFILE_ID | ID of the specific browser profile | Use DevTools or Postman) |
Troubleshooting
- Ensure the Indigo X agent is connected before running the script.
- Verify Playwright compatibility with the Mimic core version in the release notes.
- If authentication fails, double-check your
email
/USERNAME
andpassword
/PASSWORD
. - For network errors, ensure the Indigo X API endpoints (
api.indigobrowser.com
andlauncher.indigobrowser.com
) are accessible.