Tài Liệu Tham Khảo API Tự Động Hóa
Dùng REST API và giao diện tương thích ChromeDriver của NestBrowser để điều khiển môi trường trình duyệt qua Selenium, Puppeteer, Playwright hoặc bất kỳ framework tự động hóa nào.
NestBrowser cung cấp REST API cục bộ để điều khiển môi trường trình duyệt bằng code. API tương thích chuẩn ChromeDriver nên có thể dùng với Selenium, Puppeteer, Playwright và hầu hết mọi framework tự động hóa.
🔒 Tính năng API chỉ có trong gói Chuyên nghiệp và Doanh nghiệp.
Cách Hoạt Động
- NestBrowser chạy một HTTP service cục bộ (mặc định:
http://localhost:19222) - Gọi API để khởi động môi trường — NestBrowser khởi động trình duyệt và trả về URL debug WebSocket
- Code tự động hóa kết nối đến URL đó qua giao thức WebDriver tiêu chuẩn
- Trình duyệt chạy với đầy đủ cấu hình dấu vân tay NestBrowser
Bật API Service
- Mở NestBrowser → Cài đặt → API
- Bật Kích hoạt API
- Ghi lại cổng API (mặc định: 19222)
Cách Dùng API Cơ Bản
Khởi Động Môi Trường
POST http://localhost:19222/api/v1/browser/start
Content-Type: application/json
{
"profile_id": "ID môi trường của bạn"
}
Phản hồi:
{
"code": 0,
"data": {
"ws": {
"puppeteer": "ws://localhost:9222/devtools/browser/abc123",
"selenium": "ws://localhost:9222/devtools/browser/abc123"
},
"http": "http://localhost:9222",
"webdriver": "http://localhost:9222"
}
}
Dừng Môi Trường
POST http://localhost:19222/api/v1/browser/stop
Content-Type: application/json
{
"profile_id": "ID môi trường của bạn"
}
Liệt Kê Tất Cả Môi Trường
GET http://localhost:19222/api/v1/profiles
Dùng Với Puppeteer
const puppeteer = require('puppeteer-core');
async function main() {
// 1. Khởi động môi trường qua NestBrowser API
const response = await fetch('http://localhost:19222/api/v1/browser/start', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ profile_id: 'ID môi trường của bạn' })
});
const { data } = await response.json();
// 2. Kết nối Puppeteer với trình duyệt đang chạy
const browser = await puppeteer.connect({
browserWSEndpoint: data.ws.puppeteer,
defaultViewport: null
});
// 3. Điều khiển trình duyệt
const page = await browser.newPage();
await page.goto('https://example.com');
console.log(await page.title());
// 4. Dừng môi trường sau khi xong
await browser.disconnect();
await fetch('http://localhost:19222/api/v1/browser/stop', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ profile_id: 'ID môi trường của bạn' })
});
}
main();
Dùng Với Selenium
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def start_profile(profile_id):
result = requests.post(
f"http://localhost:19222/api/v1/browser/start?id={profile_id}"
)
return result.json()["ws"]["selenium"]
debugger_addr = start_profile("your-profile-id")
options = Options()
options.add_experimental_option("debuggerAddress", debugger_addr)
driver = webdriver.Chrome(options=options)
driver.get("https://example.com")
print(driver.title)
driver.quit()
Danh Sách Đầy Đủ Endpoint API
| Phương Thức | Endpoint | Mô Tả |
|---|---|---|
| GET | /api/v1/profiles | Liệt kê tất cả môi trường |
| POST | /api/v1/browser/start | Khởi động môi trường |
| POST | /api/v1/browser/stop | Dừng môi trường |
| GET | /api/v1/browser/list | Lấy danh sách môi trường đang chạy |
| POST | /api/v1/browser/cookies/import | Nhập Cookie |
| GET | /api/v1/browser/cookies/export | Xuất Cookie |
Mã Lỗi
| Mã | Ý Nghĩa |
|---|---|
| 0 | Thành công |
| 1001 | Môi trường không tồn tại |
| 1002 | Môi trường đang chạy |
| 1003 | Kết nối proxy thất bại |
| 5000 | Lỗi máy chủ nội bộ |