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. JUC之Callable接口回顾和JUC辅助类

    Callable接口和JUC辅助类 Callable接口: 回顾: 创建线程的四种方式: 继承Thread 实现runnable接口 实现callable接口 使用线程池 之前的文章:多线程编程1-定 ...

  2. Nginx_配置文件nginx.conf配置详解

    user nginx nginx ; # Nginx用户及组:用户 组.window下不指定 worker_processes 8; # 工作进程:数目.根据硬件调整,通常等于CPU数量或者2倍于CP ...

  3. vscode 前端好用插件汇总

    本篇文章根据实际开发中使用的扩展插件,结合<精选!15 个必备的 VSCode 插件(前端类)>.<vscode 插件推荐 - 献给所有前端工程师(2017.8.18更新)>中 ...

  4. CTF-sql-order by盲注

    本文章只讨论了order by盲注,关于order by的报错等注入不在本文章讨论范围,另有文章. 让我们先来看看本文章所使用的表的内容,如下图: 接下来先了解一下order by的基础知识: ord ...

  5. JAVA实现对阿里云DNS的解析管理

    1.阿里云DNS的SDK依赖 <dependency> <groupId>com.aliyun</groupId> <artifactId>tea-op ...

  6. java string 转化为json_java String 转Json报错

    缺少jar包依赖: java.lang.NoClassDefFoundError: org/apache/commons/beanutils/DynaBean 缺少commons-beanutils- ...

  7. 【初体验】macos下android ndk交叉编译hello world,并拷贝到android手机上执行

    1.机器上以前安装了java 1.8(貌似android ndk不需要java) 2. 下载android ndk,版本是android-ndk-r14b (比较奇怪,我下载了最新的android-n ...

  8. 关于new Date总结及注意事项

    记录关于 new Date() 的一些常用方法及问题 new Date()基本方法: 创建一个日期对象的几种方法 注意: 由于浏览器差异和不一致性,强烈建议不要使用Date构造函数(和Date.par ...

  9. Elasticsearch使用系列-ES增删查改基本操作+ik分词

    Elasticsearch使用系列-ES简介和环境搭建 Elasticsearch使用系列-ES增删查改基本操作+ik分词 一.安装可视化工具Kibana ES是一个NoSql数据库应用.和其他数据库 ...

  10. kindle序列号对应版本

    序列号前缀 型号全称 型号简称 支持越狱 B001, Kindle 1 K1 - B101 B002 Kindle 2 U.S. (Sprint) K2 - B003 Kindle 2 Interna ...