Bỏ qua để đến nội dung
MultiloginTrợ giúp

Ví dụ về tự động hóa Playwright

Dùng Playwright để tự động hóa hồ sơ trình duyệt Mimic trong Multilogin bằng JavaScript hoặc Python. Ví dụ bên dưới dùng API token Multilogin để khởi động một hồ sơ có sẵn với Playwright được bật, kết nối Playwright với trình duyệt đang chạy, mở trang web và lưu ả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

Để bắt đầu tự động hóa, trước tiên hãy xem các tài nguyên này: Bắt đầu với tập lệnh tự động hóa.

Trước khi bạn bắt đầu

JavaScript

  1. Tải xuống Node.js từ trang web chính thức và cài đặt nó
  2. Đảm bảo Node.js và npm (Node Package Manager) được cài đặt chính xác:
node -v 
npm -v
  1. 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
  1. Cài đặt Playwright làm phần phụ thuộc cho dự án của bạn:
npm install playwright
  1. Cài đặt Axios:
npm install axios
  1. Chèn giá trị của bạn vào các biến bên dưới trong tập lệnh:
    1. API_TOKEN: API token Multilogin của bạn
    2. folder_id, profile_id: tìm các giá trị này bằng cách sử dụng hướng dẫn của chúng tôi trên DevTools (qua giao diện) hoặc Postman

Python

  1. Cài đặt các thư viện Python sau:
    1. requests
    2. playwright
  2. Cài đặt các nhị phân trình duyệt cần thiết:
playwright install
  1. Chèn giá trị của bạn vào các biến bên dưới trong tập lệnh:
    1. API_TOKEN: API token Multilogin của bạn
    2. FOLDER_ ID, PROFILE_ ID: tìm các giá trị này bằng cách sử dụng hướng dẫn của chúng tôi trên DevTools (qua giao diện) hoặc Postman

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

  1. Đảm bảo ứng dụng được khởi chạy vì nó giúp khởi chạy hồ sơ dễ dàng
  2. Đả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
  3. Chạy tệp .js bằ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

  1. Đảm bảo ứng dụng được khởi chạy vì nó giúp khởi chạy hồ sơ dễ dàng
  2. Đả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
  3. Chạy tệp .py vớ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()

Luôn cập nhật

Cập nhật tin mới, chia sẻ kinh nghiệm và xem cách dùng thực tế.

Nhận tin sản phẩmKênh TelegramHỏi người khácChat TelegramXem cách dùngCộng đồng Discord