page-agent
Page Agent 集成助手
帮助在 Web 项目中快速集成阿里巴巴 Page Agent,实现自然语言控制网页界面。
核心概念
Page Agent 是运行在网页内的 JavaScript Agent,不是浏览器自动化工具:
- ✅ 可以操作已加载网页的 DOM
- ❌ 无法自己打开新网页或导航
- ✅ 支持自然语言控制 UI
- ❌ 不能替代 Selenium/Playwright
快速集成
方式 1: CDN 一行代码(测试用)
<script src="https://cdn.jsdelivr.net/npm/page-agent@1.5.8/dist/iife/page-agent.demo.js" crossorigin="true"></script>
⚠️ 注意: 使用 Demo LLM,仅适合技术评估,需遵守使用条款。
方式 2: NPM 安装(生产推荐)
npm install page-agent
基础配置
TypeScript/JavaScript
import { PageAgent } from 'page-agent'
const agent = new PageAgent({
model: 'qwen-max', // 或 gpt-4o, claude-3-5-sonnet-20241022 等
baseURL: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
apiKey: 'YOUR_API_KEY',
language: 'zh-CN', // 或 'en-US'
})
await agent.execute('点击登录按钮')
Vue 3 组件示例
<template>
<div>
<input v-model="userInput" placeholder="输入指令..." @keyup.enter="execute">
<button @click="execute">执行</button>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { PageAgent } from 'page-agent'
const agent = ref(null)
const userInput = ref('')
onMounted(() => {
agent.value = new PageAgent({
model: 'qwen-max',
baseURL: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
apiKey: 'YOUR_API_KEY',
language: 'zh-CN',
})
})
const execute = async () => {
await agent.value.execute(userInput.value)
}
</script>
React 组件示例
import { useEffect, useState, useRef } from 'react'
import { PageAgent } from 'page-agent'
export function PageAgentComponent() {
const agentRef = useRef<PageAgent | null>(null)
const [input, setInput] = useState('')
useEffect(() => {
agentRef.current = new PageAgent({
model: 'gpt-4o',
baseURL: 'https://api.openai.com/v1',
apiKey: 'YOUR_API_KEY',
language: 'en-US',
})
}, [])
const handleExecute = async () => {
await agentRef.current?.execute(input)
}
return (
<div>
<input
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={(e) => e.key === 'Enter' && handleExecute()}
placeholder="Enter command..."
/>
<button onClick={handleExecute}>Execute</button>
</div>
)
}
LLM 配置
阿里云通义千问
const agent = new PageAgent({
model: 'qwen-max', // qwen-max, qwen-plus, qwen-turbo
baseURL: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
apiKey: 'sk-xxx',
language: 'zh-CN',
})
OpenAI
const agent = new PageAgent({
model: 'gpt-4o', // gpt-4o, gpt-4o-mini, gpt-3.5-turbo
baseURL: 'https://api.openai.com/v1',
apiKey: 'sk-xxx',
language: 'en-US',
})
Claude
const agent = new PageAgent({
model: 'claude-3-5-sonnet-20241022',
baseURL: 'https://api.anthropic.com/v1',
apiKey: 'sk-ant-xxx',
language: 'en-US',
})
自定义 OpenAI 兼容 API
const agent = new PageAgent({
model: 'your-model-name',
baseURL: 'https://your-api-endpoint/v1',
apiKey: 'your-api-key',
language: 'zh-CN',
})
常见场景实现
智能表单填写
// 用户说:"帮我填写注册表单,姓名张三,邮箱zhang@example.com"
await agent.execute('填写注册表单:姓名张三,邮箱zhang@example.com')
// 或者分步引导
await agent.execute('找到注册表单的姓名输入框')
await agent.execute('填写张三到姓名输入框')
await agent.execute('填写 zhang@example.com 到邮箱输入框')
人机确认机制
const agent = new PageAgent({
model: 'gpt-4o',
baseURL: 'https://api.openai.com/v1',
apiKey: 'YOUR_API_KEY',
language: 'en-US',
onBeforeAction: async (action) => {
// 在执行操作前请求用户确认
const confirmed = confirm(`即将执行: ${action.description}\n确认继续?`)
return confirmed
},
})
多语言支持
// 中文环境
const agentZh = new PageAgent({
model: 'qwen-max',
baseURL: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
apiKey: 'YOUR_API_KEY',
language: 'zh-CN', // 指令使用中文
})
// 英文环境
const agentEn = new PageAgent({
model: 'gpt-4o',
baseURL: 'https://api.openai.com/v1',
apiKey: 'YOUR_API_KEY',
language: 'en-US', // 指令使用英文
})
错误处理
try {
await agent.execute('点击提交按钮')
} catch (error) {
if (error.message.includes('element not found')) {
console.error('未找到目标元素')
// 提供更友好的错误提示或备选方案
} else if (error.message.includes('permission denied')) {
console.error('操作被拒绝,可能需要权限')
}
}
Chrome 扩展集成
跨页面任务需要安装 Chrome 扩展:
# 安装扩展(从 Page Agent 官方文档获取)
# 在 manifest.json 中配置权限
扩展配置示例:
{
"manifest_version": 3,
"permissions": ["activeTab", "scripting"],
"background": {
"service_worker": "background.js"
}
}
最佳实践
- API Key 安全: 不要在前端硬编码 API Key,使用环境变量或后端代理
- 人机确认: 对于敏感操作,始终使用
onBeforeAction请求用户确认 - 错误处理: 捕获所有可能的错误,提供友好的用户体验
- 语言匹配:
language参数应与用户界面语言一致 - 性能优化: 避免频繁执行复杂操作,考虑批量处理
限制与注意事项
| 限制 | 说明 |
|---|---|
| 无法打开新页面 | 只能在已加载的网页内工作 |
| 跨域限制 | 受浏览器同源策略限制 |
| DOM 依赖 | 依赖网页的 DOM 结构,结构变化可能影响效果 |
| LLM 成本 | 每次操作都会调用 LLM,产生 API 费用 |
| 不适合自动化测试 | 不是为测试框架设计,无法替代 Cypress/Playwright |
参考资源
故障排查
问题: 找不到元素
- 检查元素是否在页面上可见
- 确认元素选择器是否正确
- 尝试更具体的描述(如"红色提交按钮"而非"提交按钮")
问题: API 调用失败
- 验证 API Key 是否正确
- 检查网络连接
- 确认 LLM 服务可用
问题: 操作不响应
- 确认
execute()被 await - 检查控制台错误信息
- 验证网页 DOM 结构是否稳定
More from echoleesong/claude-skills-plugin
chinese-novelist
|
28md-to-pptx
Convert Markdown documents to PowerPoint presentations or generate presentations from scratch using AI. Use when users want to create PPT/PPTX files, convert MD to slides, generate presentations, make slideshows, or ask for help with PowerPoint creation. Supports custom templates, multiple themes (business, tech_dark, education, neumorphism), and intelligent content layout.
26find-skills
Helps users discover and install capabilities from the open agent skills ecosystem. Use when users ask "how do I do X" for specialized tasks, request "find a skill for X", want to extend agent capabilities, or need help with specific domains (testing, design, deployment, etc.).
11skill-evolution
Skill 经验进化管理器。在对话结束时分析用户反馈,提取最佳实践和解决方案,持久化到 evolution.json 并缝合到 SKILL.md。支持三层架构(上游层、全局层、项目层)实现跨项目经验管理。当用户说"复盘"、"记录经验"、"保存这个技巧"、"evolve"时使用此工具。
9skill-manager
Skill 生命周期管理器。用于批量扫描 Skills 目录、检查 GitHub 更新、执行版本升级、管理 Skills 库存、管理经验层(promote/pull)。当用户需要"检查更新"、"列出 Skills"、"删除 Skill"、"管理 Skills"、"提升经验"、"拉取经验"时使用此工具。
8n8n-expression-syntax
Validate n8n expression syntax and fix common errors. Use when writing n8n expressions, using {{}} syntax, accessing $json/$node variables, troubleshooting expression errors, or working with webhook data in workflows.
8