Playwright automation example
Use Playwright to automate a Multilogin Mimic browser profile with JavaScript or Python. The example below uses your Multilogin API token to start an existing browser profile with Playwright enabled, connects Playwright to the running profile, opens a webpage, and saves a screenshot.
Keep the Multilogin app running while you execute the script. Playwright works with Mimic browser profiles; legacy Stealthfox profiles don't support Playwright.
You can automate Mimic browser profiles using Playwright for your web-scrapers. Note that Playwright for Stealthfox is not available in Multilogin.
To start automating, check these resources first: Getting started with automation scripting.
Before you start
JavaScript
- Download Node.js from the official website and install it
- Ensure Node.js and npm (Node Package Manager) are installed correctly:
node -v
npm -v
- Create a project directory, then run this command to initialize a new Node.js project and create a
package.jsonfile:
npm init -y
- Install Playwright as a dependency for your project:
npm install playwright
- Install Axios:
npm install axios
Python
- Install the following Python libraries:
- requests
- playwright
- Install the necessary browser binaries:
playwright install
Need the profile ID? You can copy it directly from the Multilogin interface. Learn how: How to copy profile IDs in Multilogin.
Running the script
JavaScript
- Launch the app, as it makes profile launching possible
- Make sure Playwright is compatible with the current Mimic core version – check the release notes for Playwright and Mimic
- Run the
.jsfile with your automation code
Script example
const { chromium } = require('playwright');
const axios = require('axios');
const HEADERS = {
"Content-Type": "application/json",
"Accept": "application/json",
};
// Insert your automation token in the variable below
const API_TOKEN = "";
// Insert the Folder ID and the Profile ID below
const folder_id = "";
const profile_id = "";
async function start_browserProfile(){
// Add the automation token to the request headers
HEADERS.Authorization = 'Bearer ' + API_TOKEN;
// Launch a profile defining "Playwright" as automation type
const profileLaunch_URL = `https://launcher.mlx.yt:45001/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}`;
// if you prefer to connect with browserWSEndpoint, try to get the webSocketDebuggerUrl by following request
// const {data : {webSocketDebuggerUrl}} = await axios.get(`${browserURL}/json/version`)
const browser = await chromium.connectOverCDP(browserURL,{timeout:10000});
const context = browser.contexts()[0];
const page = await context.newPage();
await page.goto("https://multilogin.com/");
await page.screenshot({path: "example.png"});
await page.close();
// Disconnect Playwright so the script can exit
await browser.close();
// Stop the profile
await axios.get(`https://launcher.mlx.yt:45001/api/v1/profile/stop/p/${profile_id}`, { headers: HEADERS });
} catch (error) {
console.log("Error:", error.message);
if (error.response) {
console.log("Response data:", error.response.data);
}
}
};
start_browserProfile();Python
- Launch the app, as it makes profile launching possible
- Make sure Playwright is compatible with the current Mimic core version – check the release notes for Playwright and Mimic
- Run the
.pyfile with your automation code
Script example
import requests
import time
from playwright.sync_api import sync_playwright
HEADERS = {"Accept": "application/json", "Content-Type": "application/json"}
# TODO: Insert your automation token in the variable below
API_TOKEN = ""
# TODO: Insert the Folder ID and the Profile ID below
FOLDER_ID = ""
PROFILE_ID = ""
HEADERS["Authorization"] = f"Bearer {API_TOKEN}"
def start_profile():
with sync_playwright() as pw:
resp = requests.get(
f"https://launcher.mlx.yt:45001/api/v2/profile/f/{FOLDER_ID}/p/{PROFILE_ID}/start?automation_type=playwright&headless_mode=false",
headers=HEADERS)
resp_json = resp.json()
if resp.status_code != 200:
print(f"\nError while starting profile: {resp.text}\n")
return
else:
print(f"\nProfile {PROFILE_ID} started.\n")
browserPort = resp_json["data"]["port"]
browserURL = f"http://127.0.0.1:{browserPort}"
# if you prefer to connect with browserWSEndpoint, try to get the webSocketDebuggerUrl by following request
# response = requests.get(f'{browserURL}/json/version')
# browser_ws_endpoint = response.json()["webSocketDebuggerUrl"]
browser = pw.chromium.connect_over_cdp(endpoint_url=browserURL)
context = browser.contexts[0]
page = context.new_page()
page.goto('https://www.multilogin.com')
time.sleep(5)
page.screenshot(path='example.png')
page.close()
browser.close()
# Stop the profile
requests.get(f"https://launcher.mlx.yt:45001/api/v1/profile/stop/p/{PROFILE_ID}", headers=HEADERS)
print(f"\nProfile {PROFILE_ID} stopped.\n")
start_profile()