puppeteer-automation by mindrally/skills
npx skills add https://github.com/mindrally/skills --skill puppeteer-automation您是 Puppeteer、Node.js 浏览器自动化、网页抓取以及为 Chrome 和 Chromium 浏览器构建可靠自动化脚本方面的专家。
npm init -y
npm install puppeteer
const puppeteer = require('puppeteer');
async function main() {
const browser = await puppeteer.launch({
headless: 'new',
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
try {
const page = await browser.newPage();
await page.goto('https://example.com');
// 你的自动化代码写在这里
} finally {
await browser.close();
}
}
main().catch(console.error);
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
const browser = await puppeteer.launch({
headless: 'new', // 'new' 表示新的无头模式,false 表示可见浏览器
slowMo: 50, // 减慢操作速度以便调试
devtools: true, // 自动打开开发者工具
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-accelerated-2d-canvas',
'--disable-gpu',
'--window-size=1920,1080'
],
defaultViewport: {
width: 1920,
height: 1080
}
});
// 导航到 URL
await page.goto('https://example.com', {
waitUntil: 'networkidle2', // 等待直到网络空闲
timeout: 30000
});
// 等待选项:
// - 'load': 等待 load 事件
// - 'domcontentloaded': 等待 DOMContentLoaded 事件
// - 'networkidle0': 500 毫秒内没有网络连接
// - 'networkidle2': 500 毫秒内不超过 2 个网络连接
// 后退/前进导航
await page.goBack();
await page.goForward();
// 重新加载页面
await page.reload({ waitUntil: 'networkidle2' });
// 单个元素
const element = await page.$('selector');
// 多个元素
const elements = await page.$$('selector');
// 等待元素出现
const element = await page.waitForSelector('selector', {
visible: true,
timeout: 5000
});
// XPath 选择
const elements = await page.$x('//xpath/expression');
// 获取文本内容
const text = await page.$eval('selector', el => el.textContent);
// 获取属性
const href = await page.$eval('a', el => el.getAttribute('href'));
// 多个元素
const texts = await page.$$eval('.items', elements =>
elements.map(el => el.textContent)
);
// 执行任意 JavaScript
const result = await page.evaluate(() => {
return document.title;
});
await page.click('button#submit');
// 带选项的点击
await page.click('button', {
button: 'left', // 'left', 'right', 'middle'
clickCount: 1,
delay: 100 // 鼠标按下和松开之间的时间
});
// 点击并等待导航
await Promise.all([
page.waitForNavigation(),
page.click('a.nav-link')
]);
// 输入文本
await page.type('input#username', 'myuser', { delay: 50 });
// 清除并输入
await page.click('input#username', { clickCount: 3 });
await page.type('input#username', 'newvalue');
// 按键
await page.keyboard.press('Enter');
await page.keyboard.down('Shift');
await page.keyboard.press('Tab');
await page.keyboard.up('Shift');
// 选择下拉菜单
await page.select('select#country', 'us');
// 勾选复选框
await page.click('input[type="checkbox"]');
// 文件上传
const inputFile = await page.$('input[type="file"]');
await inputFile.uploadFile('/path/to/file.pdf');
// 等待选择器
await page.waitForSelector('.loaded');
// 等待选择器消失
await page.waitForSelector('.loading', { hidden: true });
// 等待函数条件
await page.waitForFunction(
() => document.querySelector('.count').textContent === '10'
);
// 等待导航
await page.waitForNavigation({ waitUntil: 'networkidle2' });
// 等待网络请求
await page.waitForRequest(request =>
request.url().includes('/api/data')
);
// 等待网络响应
await page.waitForResponse(response =>
response.url().includes('/api/data') && response.status() === 200
);
// 固定超时(谨慎使用)
await page.waitForTimeout(1000);
// 全页截图
await page.screenshot({
path: 'screenshot.png',
fullPage: true
});
// 元素截图
const element = await page.$('.chart');
await element.screenshot({ path: 'chart.png' });
// 截图选项
await page.screenshot({
path: 'screenshot.png',
type: 'png', // 'png' 或 'jpeg'
quality: 80, // 仅限 jpeg,0-100
clip: {
x: 0,
y: 0,
width: 800,
height: 600
}
});
await page.pdf({
path: 'document.pdf',
format: 'A4',
printBackground: true,
margin: {
top: '20px',
right: '20px',
bottom: '20px',
left: '20px'
}
});
// 启用请求拦截
await page.setRequestInterception(true);
page.on('request', request => {
// 阻止图片和样式表
if (['image', 'stylesheet'].includes(request.resourceType())) {
request.abort();
} else {
request.continue();
}
});
// 修改请求
page.on('request', request => {
request.continue({
headers: {
...request.headers(),
'X-Custom-Header': 'value'
}
});
});
// 监控响应
page.on('response', async response => {
if (response.url().includes('/api/')) {
const data = await response.json();
console.log('API 响应:', data);
}
});
// 基本 HTTP 身份验证
await page.authenticate({
username: 'user',
password: 'pass'
});
// 设置 Cookies
await page.setCookie({
name: 'session',
value: 'abc123',
domain: 'example.com'
});
// 获取 Cookies
const cookies = await page.cookies();
// 清除 Cookies
await page.deleteCookie({ name: 'session' });
// 创建隐身上下文
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
// 多个页面
const page1 = await browser.newPage();
const page2 = await browser.newPage();
// 获取所有页面
const pages = await browser.pages();
// 处理弹出窗口
page.on('popup', async popup => {
await popup.waitForLoadState();
console.log('弹出窗口 URL:', popup.url());
});
async function scrapeWithRetry(url, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// 设置超时
page.setDefaultTimeout(30000);
await page.goto(url, { waitUntil: 'networkidle2' });
const data = await page.$eval('.content', el => el.textContent);
await browser.close();
return data;
} catch (error) {
console.error(`尝试 ${i + 1} 失败:`, error.message);
if (i === maxRetries - 1) throw error;
await new Promise(r => setTimeout(r, 2000 * (i + 1)));
}
}
}
// 禁用不必要的功能
await page.setRequestInterception(true);
page.on('request', request => {
const blockedTypes = ['image', 'stylesheet', 'font'];
if (blockedTypes.includes(request.resourceType())) {
request.abort();
} else {
request.continue();
}
});
// 重用浏览器实例
const browser = await puppeteer.launch();
async function scrape(url) {
const page = await browser.newPage();
try {
await page.goto(url);
// ... 抓取逻辑
} finally {
await page.close(); // 关闭页面,而非浏览器
}
}
// 使用连接池进行并行抓取
const cluster = require('puppeteer-cluster');
waitForSelectornetworkidle2 而非 networkidle0每周安装量
486
代码仓库
GitHub 星标数
42
首次出现
2026年1月25日
安全审计
安装于
gemini-cli419
opencode418
codex405
github-copilot400
cursor396
amp372
You are an expert in Puppeteer, Node.js browser automation, web scraping, and building reliable automation scripts for Chrome and Chromium browsers.
npm init -y
npm install puppeteer
const puppeteer = require('puppeteer');
async function main() {
const browser = await puppeteer.launch({
headless: 'new',
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
try {
const page = await browser.newPage();
await page.goto('https://example.com');
// Your automation code here
} finally {
await browser.close();
}
}
main().catch(console.error);
const browser = await puppeteer.launch({
headless: 'new', // 'new' for new headless mode, false for visible browser
slowMo: 50, // Slow down operations for debugging
devtools: true, // Open DevTools automatically
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-accelerated-2d-canvas',
'--disable-gpu',
'--window-size=1920,1080'
],
defaultViewport: {
width: 1920,
height: 1080
}
});
// Navigate to URL
await page.goto('https://example.com', {
waitUntil: 'networkidle2', // Wait until network is idle
timeout: 30000
});
// Wait options:
// - 'load': Wait for load event
// - 'domcontentloaded': Wait for DOMContentLoaded event
// - 'networkidle0': No network connections for 500ms
// - 'networkidle2': No more than 2 network connections for 500ms
// Navigate back/forward
await page.goBack();
await page.goForward();
// Reload page
await page.reload({ waitUntil: 'networkidle2' });
// Single element
const element = await page.$('selector');
// Multiple elements
const elements = await page.$$('selector');
// Wait for element
const element = await page.waitForSelector('selector', {
visible: true,
timeout: 5000
});
// XPath selection
const elements = await page.$x('//xpath/expression');
// Get text content
const text = await page.$eval('selector', el => el.textContent);
// Get attribute
const href = await page.$eval('a', el => el.getAttribute('href'));
// Multiple elements
const texts = await page.$$eval('.items', elements =>
elements.map(el => el.textContent)
);
// Execute arbitrary JavaScript
const result = await page.evaluate(() => {
return document.title;
});
await page.click('button#submit');
// Click with options
await page.click('button', {
button: 'left', // 'left', 'right', 'middle'
clickCount: 1,
delay: 100 // Time between mousedown and mouseup
});
// Click and wait for navigation
await Promise.all([
page.waitForNavigation(),
page.click('a.nav-link')
]);
// Type text
await page.type('input#username', 'myuser', { delay: 50 });
// Clear and type
await page.click('input#username', { clickCount: 3 });
await page.type('input#username', 'newvalue');
// Press keys
await page.keyboard.press('Enter');
await page.keyboard.down('Shift');
await page.keyboard.press('Tab');
await page.keyboard.up('Shift');
// Select dropdown
await page.select('select#country', 'us');
// Check checkbox
await page.click('input[type="checkbox"]');
// File upload
const inputFile = await page.$('input[type="file"]');
await inputFile.uploadFile('/path/to/file.pdf');
// Wait for selector
await page.waitForSelector('.loaded');
// Wait for selector to disappear
await page.waitForSelector('.loading', { hidden: true });
// Wait for function
await page.waitForFunction(
() => document.querySelector('.count').textContent === '10'
);
// Wait for navigation
await page.waitForNavigation({ waitUntil: 'networkidle2' });
// Wait for network request
await page.waitForRequest(request =>
request.url().includes('/api/data')
);
// Wait for network response
await page.waitForResponse(response =>
response.url().includes('/api/data') && response.status() === 200
);
// Fixed timeout (use sparingly)
await page.waitForTimeout(1000);
// Full page screenshot
await page.screenshot({
path: 'screenshot.png',
fullPage: true
});
// Element screenshot
const element = await page.$('.chart');
await element.screenshot({ path: 'chart.png' });
// Screenshot options
await page.screenshot({
path: 'screenshot.png',
type: 'png', // 'png' or 'jpeg'
quality: 80, // jpeg only, 0-100
clip: {
x: 0,
y: 0,
width: 800,
height: 600
}
});
await page.pdf({
path: 'document.pdf',
format: 'A4',
printBackground: true,
margin: {
top: '20px',
right: '20px',
bottom: '20px',
left: '20px'
}
});
// Enable request interception
await page.setRequestInterception(true);
page.on('request', request => {
// Block images and stylesheets
if (['image', 'stylesheet'].includes(request.resourceType())) {
request.abort();
} else {
request.continue();
}
});
// Modify requests
page.on('request', request => {
request.continue({
headers: {
...request.headers(),
'X-Custom-Header': 'value'
}
});
});
// Monitor responses
page.on('response', async response => {
if (response.url().includes('/api/')) {
const data = await response.json();
console.log('API Response:', data);
}
});
// Basic HTTP authentication
await page.authenticate({
username: 'user',
password: 'pass'
});
// Set cookies
await page.setCookie({
name: 'session',
value: 'abc123',
domain: 'example.com'
});
// Get cookies
const cookies = await page.cookies();
// Clear cookies
await page.deleteCookie({ name: 'session' });
// Create incognito context
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
// Multiple pages
const page1 = await browser.newPage();
const page2 = await browser.newPage();
// Get all pages
const pages = await browser.pages();
// Handle popups
page.on('popup', async popup => {
await popup.waitForLoadState();
console.log('Popup URL:', popup.url());
});
async function scrapeWithRetry(url, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Set timeout
page.setDefaultTimeout(30000);
await page.goto(url, { waitUntil: 'networkidle2' });
const data = await page.$eval('.content', el => el.textContent);
await browser.close();
return data;
} catch (error) {
console.error(`Attempt ${i + 1} failed:`, error.message);
if (i === maxRetries - 1) throw error;
await new Promise(r => setTimeout(r, 2000 * (i + 1)));
}
}
}
// Disable unnecessary features
await page.setRequestInterception(true);
page.on('request', request => {
const blockedTypes = ['image', 'stylesheet', 'font'];
if (blockedTypes.includes(request.resourceType())) {
request.abort();
} else {
request.continue();
}
});
// Reuse browser instance
const browser = await puppeteer.launch();
async function scrape(url) {
const page = await browser.newPage();
try {
await page.goto(url);
// ... scraping logic
} finally {
await page.close(); // Close page, not browser
}
}
// Use connection pool for parallel scraping
const cluster = require('puppeteer-cluster');
waitForSelector before interacting with elementsnetworkidle2 over networkidle0 for faster loadsWeekly Installs
486
Repository
GitHub Stars
42
First Seen
Jan 25, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
gemini-cli419
opencode418
codex405
github-copilot400
cursor396
amp372
Gmail过滤器创建教程 - 使用Google Workspace CLI自动分类邮件与添加标签
6,500 周安装
React前端开发模式实战:组件组合、复合组件与渲染属性模式详解
464 周安装
Mermaid 可视化工具 - 自动生成专业图表,优化文档与演示,兼容 Obsidian/GitHub
465 周安装
OpenAI API 完整指南:GPT-5、GPT-4o、DALL-E 3、Whisper 集成与Node.js/JavaScript开发
465 周安装
客户旅程地图制作指南:5步创建跨职能客户体验地图,提升转化与忠诚度
466 周安装
Next.js 15+ 最佳实践指南:文件约定、RSC边界、异步模式与性能优化
466 周安装
Airflow 2 到 3 迁移指南:代码变更、元数据访问与自动化升级
467 周安装