What can I do?

Most things that you can do manually in the browser can be done using Puppeteer! Here are a few examples to get you started:

  1. Generate screenshots and PDFs of pages.
  2. Crawl a SPA and generate pre-rendered content (i.e. "SSR").
  3. Automate form submission, UI testing, keyboard input, etc.
  4. Create an up-to-date, automated testing environment. Run your tests directly in the latest version of Chrome using the latest JavaScript and browser features.
  5. Capture a timeline trace of your site to help diagnose performance issues.

例子说明:

const puppeteer = require('puppeteer'); // 导入puppeteer库
(async () => { //固定语法格式
const browser = await puppeteer.launch(); //根据puppeteer创建一个Browser对象,相当于启动了浏览器
//const browser = await puppeteer.launch({headless:false}); //相当于以可视的方式启动了浏览器,headless默认为true
//const browser = await puppeteer.launch({executablePath:'/Volumes/A/chrome'});//引用其它谷歌chrome版本,但兼容性很差,只适用于Chrome Dev或Chrome Canary
const page = await browser.newPage(); //根据Browser创建一个Page对象,相当于打开一个标签页
await page.goto('https://example.com'); //page.goto()跳转到指定url地址
await page.screenshot({path: 'example.png'}); //page.screenshot()对页面进行截图
await browser.close(); //关闭浏览器
})();

// Example1 - navigating to https://example.com and saving a screenshot as example.png.截取网页为png图片

const puppeteer = require("puppeteer");
(async () =>{
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path:'example.png'});
await browser.close();
})();

// Example2 - create a PDF.将网页生存PDF格式文档

const puppeteer = require('puppeteer');

(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
//await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle2'});
await page.goto('https://www.baidu.com', {waitUntil: 'networkidle2'});
await page.pdf({path: 'hn0.pdf', format: 'A4'}); await browser.close();
})();

//Example3 - evaluate script in the context of the page,打印网页信息

const puppeteer = require('puppeteer');

(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com'); // Get the "viewport" of the page, as reported by the page.
const dimensions = await page.evaluate(() => {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
deviceScaleFactor: window.devicePixelRatio
};
}); console.log('Dimensions:', dimensions); await browser.close();
})();

puppeteerExamples的更多相关文章

随机推荐

  1. spring security 登出操作 详细说明

    1.前言 这里专门 做 spring security 登出操作 的  详细记录 2.操作 (1)目录结构 (2)在security 拦截规则配置文件添加退出登录支持 源码 package com.e ...

  2. StringBuffer和String的区别

    面试题:String为什么不可变 StringBuffer和StringBuilder的区别 String 和StringBuffer的区别: (一):String 类中的byte数组使用final修 ...

  3. markdown mermaid状态图

    状态图 状态图是一种用于计算机科学和相关领域描述系统行为的图.状态图要求描述的系统由有限数量的状态组成. 语法: stateDiagram-v2 [*] --> Still Still --&g ...

  4. python分支结构与循环结构

    python分支结构 一.if 单条件形式 # 年轻人的世界都不容易 age > 18 age = int(input("请输入您的年龄:")) # input()函数 模拟 ...

  5. JUC之线程池基础

    线程池 定义和方法 线程池的工作时控制运行的线程数量,处理过程中将任务放入队列,然后在线程创建后启动这些任务,如果线程数量超过了最大数量,超出数量的线程排队等候,等待其他线程执行完成,再从队列中取出任 ...

  6. 《剑指offer》面试题65. 不用加减乘除做加法

    问题描述 写一个函数,求两个整数之和,要求在函数体内不得使用 "+"."-"."*"."/" 四则运算符号. 示例: 输 ...

  7. leetcode 120. 三角形最小路径和 及 53. 最大子序和

    三角形最小路径和 问题描述 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] ...

  8. 读书笔记http之第一章

    http TCP/IP协议各层: 应用层 决定了向用户提供应用服务时通信的活动. 比如 : FTP(FileTransferProtocol,文件传输协议)和DNS(DomainNameSystem, ...

  9. 运维利器-ClusterShell集群管理

    在运维实战中,如果有若干台数据库服务器,想对这些服务器进行同等动作,比如查看它们当前的即时负载情况,查看它们的主机名,分发文件等等,这个时候该怎么办?一个个登陆服务器去操作,太傻帽了!写个shell去 ...

  10. Vue 之 vue-cropper 组件实现头像裁剪功能

    组件与api地址: npm地址地址:https://www.npmjs.com/package/vue-cropper/v/0.4.7 GitHub地址:https://github.com/xyxi ...