自動化與 RPA 整合指南

NestBrowser 提供本地 REST API,允許您以程式化方式控制瀏覽器配置檔案。這支援強大的自動化工作流——從簡單的腳本化登入,到管理數百個帳號的複雜 RPA 流程。

自動化 API 的運作原理

NestBrowser 執行時會啟動一個本地 HTTP 服務(預設:http://localhost:19222)。該服務提供以下功能的介面:

  • 列出和建立瀏覽器配置檔案
  • 啟動和停止配置檔案
  • 透過 WebSocket 偵錯連接埠連線自動化框架
  • 以程式化方式管理 Cookie 和工作階段

使用 Puppeteer 連線

Puppeteer 是最常見的 NestBrowser 整合自動化框架。

第一步:取得配置檔案的 WebSocket URL

const axios = require('axios');

// 取得配置檔案清單
const { data: profiles } = await axios.get('http://localhost:19222/api/v1/browser/list');

// 透過 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;
// 例如: "ws://127.0.0.1:9222/devtools/browser/xxx"

第二步:將 Puppeteer 連線至配置檔案

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');

// 在這裡編寫您的自動化邏輯
await page.waitForSelector('#nav-link-accountList');

第三步:自動化完成後關閉配置檔案

// 關閉配置檔案(可選——會停止瀏覽器但保留配置檔案資料)
await axios.post(`http://localhost:19222/api/v1/browser/stop?id=${profileId}`);

使用 Selenium WebDriver 連線

NestBrowser 的 API 與 ChromeDriver 相容,因此 Selenium 的接入設定很簡單。

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

# 啟動配置檔案(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"]
# 例如: "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)

使用 Playwright 連線

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

const wsEndpoint = 'ws://127.0.0.1:9222/devtools/browser/xxx'; // 從 API 取得

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

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

批次多帳號自動化

以下是同時管理多個帳號的模式:

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

async function processProfile(profileId) {
  // 啟動配置檔案
  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();
    // 您的帳號特定邏輯
    await page.goto('https://yourplatform.com/dashboard');
    // ... 自動化操作
  } finally {
    await browser.disconnect();
    await axios.post(`http://localhost:19222/api/v1/browser/stop?id=${profileId}`);
  }
}

// 並行處理(注意速率限制)
await Promise.all(profiles.map(processProfile));

最佳實踐

  • 錯誤處理:始終在 try/finally 中包裝自動化程式碼,確保配置檔案在出錯時也能正確關閉
  • 速率限制:避免同時啟動太多配置檔案,這可能影響效能並觸發平台偵測
  • 隨機延遲:在操作之間新增隨機化延遲,使行為更像人類
  • 工作階段保存:讓 NestBrowser 自動儲存工作階段,避免每次都需要重新登入