Install
openclaw skills install playwright-controllerBrowse webpages using Playwright with automatic loading wait, screenshots, and text extraction. Use playwright:fetch or playwright:screenshot commands. API: fetchWithPlaywright(url, options), fetchElementAndScreenshot(url, selector, options)
openclaw skills install playwright-controller使用 Playwright 智能浏览网页,支持截图和文本提取。
playwright:fetch <url>
自动等待 JS/CSS 加载完成,截取全屏截图,并提取页面文本内容。
特点:
示例:
playwright:fetch https://baike.baidu.com/item/兴盛优选/23451097
输出:
./screenshots/<timestamp>_<url_hash>.png./screenshots/<timestamp>_<url_hash>_content.txtplaywright:fetch --timeout=120000 --dir=/custom/path <url>
参数:
--timeout: 超时时间(毫秒),默认 60000--dir: 截图和文本文件保存目录,默认 ./screenshots<url>: 目标网页 URL示例:
playwright:fetch --timeout=120000 --dir=/Users/chenkuan/Desktop/screenshots https://baike.baidu.com/item/兴盛优选/23451097
playwright:fetch-element --selector=".content" <url>
截取指定 CSS 选择器元素的截图和文本内容。
示例:
playwright:fetch-element --selector="h1" https://baike.baidu.com/item/兴盛优选/23451097
执行后会生成以下文件:
screenshots/
├── 1716982345678_www_baike_baidu_com_item_兴盛优选_23451097.png # 全屏截图
├── 1716982345678_www_baike_baidu_com_item_兴盛优选_23451097_content.txt # 页面文本
└── 1716982345678_www_baike_baidu_com_item_兴盛优选_23451097_element.png # 元素截图
✅ 自动加载等待 - 无需手动等待 JS/CSS 加载 ✅ 智能资源拦截 - 跳过图片等无关资源,提高速度 ✅ 全屏截图 - 保存完整的页面视觉信息 ✅ 文本提取 - 适合后续文本处理和 AI 分析 ✅ 稳定可靠 - 使用有头模式便于调试 ✅ 错误处理 - 即使失败也会保存截图 ✅ 时间戳命名 - 避免文件名冲突
获取整个网页的内容和截图。
参数:
url (string, 必需): 目标网页 URLoptions (object, 可选):
headless (boolean): 是否无头模式,默认 falsetimeout (number): 超时时间(毫秒),默认 60000screenshotDir (string): 截图保存目录,默认 './screenshots'返回:
{
content: string, // 页面文本内容
screenshotPath: string, // 截图文件路径
title: string, // 页面标题
timestamp: number // 时间戳
}
工作流程:
示例:
const { fetchWithPlaywright } = require('./playwright-crawler-v3.js');
const result = await fetchWithPlaywright('https://example.com', {
headless: false,
timeout: 60000,
screenshotDir: './screenshots'
});
console.log(result.content); // 页面文本
console.log(result.screenshotPath); // 截图路径
获取指定 CSS 选择器元素的截图和文本。
参数:
url (string, 必需): 目标网页 URLselector (string, 必需): CSS 选择器(如 '.content', 'h1', '#article')options (object, 可选):
headless (boolean): 是否无头模式,默认 falsetimeout (number): 超时时间(毫秒),默认 60000screenshotDir (string): 截图保存目录,默认 './screenshots'返回:
{
content: string, // 元素文本内容
screenshotPath: string, // 元素截图文件路径
title: string, // 页面标题
timestamp: number // 时间戳
}
示例:
const { fetchElementAndScreenshot } = require('./playwright-crawler-v3.js');
const result = await fetchElementAndScreenshot(
'https://example.com',
'.article-content',
{
headless: false,
screenshotDir: './screenshots'
}
);
console.log(result.content); // 元素文本
console.log(result.screenshotPath); // 截图路径
1. 智能页面加载等待
page.goto(url, { waitUntil: 'networkidle' })2. 完整资源加载
// 不设置任何拦截规则,让所有资源正常加载
// 包括:图片、CSS、字体、JavaScript 等
// 这样截图才能看到完整的页面效果
3. 移动端模拟
const context = await browser.newContext({
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X)...',
viewport: { width: 375, height: 667 },
isMobile: true
});
3. 截图功能
page.screenshot({ fullPage: true })element.screenshot()4. 文本提取
const content = await page.evaluate(() => {
document.body.innerText; // 获取文本内容
});
5. 元素定位
// CSS 选择器
const element = await page.$('.content'); // 单个元素
const elements = await page.$$('.article'); // 多个元素
// 使用选择器
await element.$eval('h2', el => el.innerText); // 提取元素文本
headless 模式
// 有头模式(可见浏览器,适合调试)
const browser = await chromium.launch({ headless: false });
// 无头模式(后台运行,速度更快)
const browser = await chromium.launch({ headless: true });
viewport(视口大小)- 默认移动端
const context = await browser.newContext({
viewport: { width: 375, height: 800 } // 375x800
});
userAgent(浏览器标识)- 默认移动端
const context = await browser.newContext({
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1'
});
isMobile(移动设备模拟)
const context = await browser.newContext({
isMobile: true // 开启移动设备模拟
});
自动生成的文件:
screenshots/
├── 1716982345678_www_example_com_content.png # 全屏截图
├── 1716982345678_www_example_com_content.txt # 页面文本
├── 1716982345678_www_example_com_article.png # 元素截图
└── 1716982345678_www_example_com_error.png # 错误截图
文件命名规则:
https:// 并替换非字母数字字符为下划线基于 Node.js Playwright 库实现,提供以下功能:
实现文件:
playwright-cmd.js - 命令行接口playwright-crawler-v3.js - 核心抓取功能Skill 已内置所有依赖,无需额外安装:
# 直接使用即可
playwright:fetch https://example.com