掌握浏览器自动化
使用 Puppeteer、Playwright 和 Selenium 自动化 Nox Core 的完整指南。生产就绪的代码示例和最佳实践。
快速开始
5分钟内运行
代码示例
即插即用的代码片段
RPA 引擎
无代码自动化
Code Examples
Puppeteer + Chrome DevTools Protocol
Connect to Nox Core via Chrome DevTools Protocol for full browser control.
基本设置
// 1. Launch Nox Core with remote debugging
// Add flag: --remote-debugging-port=9222
const puppeteer = require('puppeteer-core');
const browser = await puppeteer.connect({
browserWSEndpoint: 'ws://localhost:9222',
defaultViewport: null
});
const page = await browser.newPage();
await page.goto('https://example.com');高级功能
// Advanced profile management
const profileId = 'your-profile-id';
// Get all available targets
const targets = await browser.targets();
const pageTarget = targets.find(t => t.type() === 'page');
// Execute CDP commands
const client = await page.target().createCDPSession();
// Set custom user agent
await client.send('Network.setUserAgentOverride', {
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
});
// Block specific resources
await client.send('Network.setBlockedURLs', {
urls: ['*.analytics.com', '*.tracking.js']
});自动化模式
// Automation best practices
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
// Human-like typing
async function humanType(page, selector, text) {
for (const char of text) {
await page.type(selector, char, { delay: Math.random() * 100 + 50 });
}
}
// Random mouse movements
async function randomMove(page) {
await page.mouse.move(
Math.random() * 800 + 100,
Math.random() * 600 + 100
);
}
// Use in your script
await randomMove(page);
await humanType(page, '#username', 'john_doe');
await delay(Math.random() * 500 + 200);
await humanType(page, '#password', 'secret123');最佳实践
避免检测
- 操作之间使用真实的延迟(200-1000毫秒)
- 随机化鼠标移动和点击位置
- 变化视口大小和用户代理
- 会话之间清除Cookie和存储
性能
- 尽可能重用浏览器实例
- 使用无头模式获得更好性能
- 阻止不必要的资源
- 实现连接池
代理管理
- 每10-20个请求轮换代理
- 将代理位置与时区匹配
- 使用前测试代理质量
- 对敏感网站使用住宅/ISP代理
错误处理
- 实现指数退避的重试逻辑
- 失败时截取屏幕截图
- 记录所有操作以便调试
- 优雅地处理验证码
RPA 引擎示例
登录自动化
使用2FA支持自动化登录流程
// Nox Core RPA - Login Flow
{
"steps": [
{ "action": "navigate", "url": "https://example.com/login" },
{ "action": "wait", "selector": "#username", "timeout": 5000 },
{ "action": "type", "selector": "#username", "value": "{{USERNAME}}" },
{ "action": "type", "selector": "#password", "value": "{{PASSWORD}}" },
{ "action": "click", "selector": "#login-btn" },
{ "action": "wait", "selector": ".dashboard", "timeout": 10000 },
{ "action": "extract", "selector": ".user-name", "variable": "loggedUser" }
]
}数据提取
从多个页面提取数据
// Nox Core RPA - Data Extraction
{
"steps": [
{ "action": "navigate", "url": "{{PRODUCT_URL}}" },
{ "action": "wait", "selector": ".product-title" },
{ "action": "extract", "selector": ".product-title", "variable": "title" },
{ "action": "extract", "selector": ".price", "variable": "price" },
{ "action": "extract", "selector": ".availability", "variable": "stock" },
{ "action": "screenshot", "name": "product_{{timestamp}}" },
{ "action": "webhook", "url": "{{API_ENDPOINT}}", "data": ["title", "price", "stock"] }
]
}表单提交
自动化复杂的表单提交
// Nox Core RPA - Form Submission
{
"steps": [
{ "action": "navigate", "url": "https://example.com/form" },
{ "action": "select", "selector": "#country", "value": "{{COUNTRY}}" },
{ "action": "type", "selector": "#fullname", "value": "{{FULLNAME}}" },
{ "action": "type", "selector": "#email", "value": "{{EMAIL}}" },
{ "action": "click", "selector": "#terms-checkbox" },
{ "action": "click", "selector": "#submit-btn" },
{ "action": "wait", "selector": ".success-message", "timeout": 15000 },
{ "action": "condition", "if": ".error", "then": [{ "action": "log", "message": "Submission failed" }] }
]
}重要通知
自动化时始终遵守网站的服务条款。负责任地使用自动化,并确保遵守当地法律法规。