Hướng Dẫn Tích Hợp Tự Động Hóa Và RPA

Tìm hiểu cách dùng API tự động hóa của NestBrowser để tích hợp với Selenium, Puppeteer, Playwright và quy trình RPA tùy chỉnh, quản lý hàng loạt hồ sơ trình duyệt.

NestBrowser cung cấp REST API cục bộ cho phép bạn điều khiển hồ sơ trình duyệt theo cách lập trình. Điều này hỗ trợ các quy trình tự động hóa mạnh mẽ — từ đăng nhập tự động đơn giản đến quy trình RPA phức tạp quản lý hàng trăm tài khoản.

Cách Thức Hoạt Động Của API Tự Động Hóa

Khi NestBrowser chạy, nó khởi động một HTTP service cục bộ (mặc định: http://localhost:19222). Service này cung cấp giao diện cho:

  • Liệt kê và tạo hồ sơ trình duyệt
  • Khởi động và dừng hồ sơ
  • Kết nối framework tự động hóa qua cổng debug WebSocket
  • Quản lý Cookie và phiên theo cách lập trình

Kết Nối Với Puppeteer

Puppeteer là framework tự động hóa phổ biến nhất để tích hợp NestBrowser.

Bước 1: Lấy WebSocket URL Của Hồ Sơ

const axios = require('axios');

// Lấy danh sách hồ sơ
const { data: profiles } = await axios.get('http://localhost:19222/api/v1/browser/list');

// Khởi động hồ sơ cụ thể theo ID
const profileId = profiles[0].id;
const { data: startResult } = await axios.post(
  `http://localhost:19222/api/v1/browser/start?id=${profileId}`
);

const wsEndpoint = startResult.ws.puppeteer;
// Ví dụ: "ws://127.0.0.1:9222/devtools/browser/xxx"

Bước 2: Kết Nối Puppeteer Với Hồ Sơ

const puppeteer = require('puppeteer-core');

const browser = await puppeteer.connect({
  browserWSEndpoint: wsEndpoint,
  defaultViewport: null,
});

const page = await browser.newPage();
await page.goto('https://www.amazon.com');

// Viết logic tự động hóa của bạn ở đây
await page.waitForSelector('#nav-link-accountList');

Bước 3: Đóng Hồ Sơ Sau Khi Hoàn Thành

// Đóng hồ sơ (tùy chọn — dừng trình duyệt nhưng giữ lại dữ liệu hồ sơ)
await axios.post(`http://localhost:19222/api/v1/browser/stop?id=${profileId}`);

Kết Nối Với Selenium WebDriver

API của NestBrowser tương thích ChromeDriver, nên tích hợp Selenium rất đơn giản.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import requests

# Khởi động hồ sơ (Python)
profile_id = "your-profile-id"
result = requests.post(f"http://localhost:19222/api/v1/browser/start?id={profile_id}")
debugger_address = result.json()["ws"]["selenium"]
# Ví dụ: "127.0.0.1:9223"

options = Options()
options.add_experimental_option("debuggerAddress", debugger_address)
driver = webdriver.Chrome(options=options)

driver.get("https://www.amazon.com")
print(driver.title)

Kết Nối Với Playwright

const { chromium } = require('playwright');

const wsEndpoint = 'ws://127.0.0.1:9222/devtools/browser/xxx'; // Lấy từ API

const browser = await chromium.connectOverCDP(wsEndpoint);
const context = browser.contexts()[0];
const page = context.pages()[0];

await page.goto('https://example.com');

Tự Động Hóa Đa Tài Khoản Hàng Loạt

Mẫu quản lý nhiều tài khoản cùng lúc:

const profiles = ['profile-id-1', 'profile-id-2', 'profile-id-3'];

async function processProfile(profileId) {
  // Khởi động hồ sơ
  const { data } = await axios.post(
    `http://localhost:19222/api/v1/browser/start?id=${profileId}`
  );
  
  const browser = await puppeteer.connect({
    browserWSEndpoint: data.ws.puppeteer,
    defaultViewport: null,
  });
  
  try {
    const page = await browser.newPage();
    // Logic tùy theo từng tài khoản
    await page.goto('https://yourplatform.com/dashboard');
    // ... Các thao tác tự động hóa
  } finally {
    await browser.disconnect();
    await axios.post(`http://localhost:19222/api/v1/browser/stop?id=${profileId}`);
  }
}

// Xử lý song song (chú ý giới hạn tốc độ)
await Promise.all(profiles.map(processProfile));

Thực Hành Tốt Nhất

  • Xử lý lỗi: Luôn bọc code tự động hóa trong try/finally để đảm bảo hồ sơ đóng đúng cách khi có lỗi
  • Giới hạn tốc độ: Tránh khởi động quá nhiều hồ sơ cùng lúc; điều này có thể ảnh hưởng hiệu suất và kích hoạt phát hiện
  • Độ trễ ngẫu nhiên: Thêm độ trễ ngẫu nhiên giữa các thao tác để hành vi giống người hơn
  • Lưu phiên: Để NestBrowser tự động lưu phiên, tránh phải đăng nhập lại mỗi lần