Ví dụ về tự động hóa Playwright
Sử dụng Playwright để tự động hóa một Multilogin Mimic profile trình duyệt bằng JavaScript hoặc Python. Ví dụ dưới đây sử dụng token Multilogin API để khởi động một profile trình duyệt hiện có với Playwright được bật, kết nối Playwright với profile đang chạy, mở một trang web và lưu một ảnh chụp màn hình.
Giữ cho ứng dụng Multilogin đang chạy trong khi bạn thực thi script. Playwright hoạt động với các profile trình duyệt Mimic; các profile Stealthfox cũ không hỗ trợ Playwright.
Bạn có thể tự động hóa cấu hình trình duyệt Mimic bằng cách sử dụng Playwright cho trình thu thập dữ liệu web của mình. Lưu ý rằng Playwright dành cho Stealthfox chưa có trong Multilogin
Trước khi bạn bắt đầu
JavaScript
- Tải xuống Node.js từ trang web chính thức và cài đặt nó
- Đảm bảo Node.js và npm (Node Package Manager) được cài đặt chính xác:
node -v
npm -v
- Tạo một thư mục dự án, sau đó chạy lệnh này để khởi tạo dự án Node.js mới và tạo tệp
package.json:
npm init -y
- Cài đặt Playwright làm phần phụ thuộc cho dự án của bạn:
npm install playwright
- Cài đặt thư viện Axios:
npm install axios
Python
- Cài đặt các thư viện Python sau:
- requests
- playwright
- Cài đặt các nhị phân trình duyệt cần thiết:
playwright install
Cần profile ID? Bạn có thể sao chép trực tiếp từ giao diện Multilogin. Tìm hiểu cách thực hiện: Cách sao chép ID hồ sơ trong Multilogin
Chạy tập lệnh
JavaScript
- Đảm bảo ứng dụng được khởi chạy vì nó giúp khởi chạy hồ sơ dễ dàng
- Đảm bảo Playwright tương thích với phiên bản lõi Mimic hiện tại – kiểm tra ghi chú phát hành cho Playwright và Mimic
- Chạy tệp
.jsbằng mã tự động hóa của bạn
Ví dụ về tập lệnh
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
- Đảm bảo ứng dụng được khởi chạy vì nó giúp khởi chạy hồ sơ dễ dàng
- Đảm bảo Playwright tương thích với phiên bản lõi Mimic hiện tại – kiểm tra ghi chú phát hành cho Playwright và Mimic
- Chạy tệp
.pyvới mã tự động hóa của bạn
Ví dụ về tập lệnh
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()