puppeteerExamples
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:
- Generate screenshots and PDFs of pages.
- Crawl a SPA and generate pre-rendered content (i.e. "SSR").
- Automate form submission, UI testing, keyboard input, etc.
- 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.
- 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的更多相关文章
随机推荐
- python_自动查找指定目录下的文件或目录的方法
代码如下 import os def find_file(search_path, file_type="file", filename=None, file_startswith ...
- Apache Ant: If 和 Unless
目录 If And Unless If And Unless 从 Ant 1.9.1 起,可以在所有的任务和嵌套的元素上以特别的命名空间添加 if 和 unless 属性. In order to u ...
- Samba服务器搭建与配置
Samba服务简介Samba的起源:对于windows的网上邻居来讲,共享文件的方式用的是SMB和CIFS协议以及NETBIOS协议Linux/Unix之间用的是NFS协议. 但是Linux和Wi ...
- Java实现163邮箱发送邮件到QQ邮箱
注:图片如果损坏,点击文章链接:https://www.toutiao.com/i6812973124141711876/ 先创建一个maven的普通项目 添加依赖,附在文档末尾 其中几个注意的地方 ...
- HIVE执行引擎TEZ学习以及实际使用
概述 最近公司在使用Tez,今天写一篇关于Tez的学习和使用随笔.Tez是Apache最新的支持DAG作业的开源计算框架,它可以将多个有依赖的作业转换为一个作业从而大幅提升DAG作业的性能.Tez并不 ...
- Android官方文档翻译 十四 3.2Supporting Different Screens
Supporting Different Screens 支持不同的屏幕 This lesson teaches you to 这节课教给你 Create Different Layouts 创建不同 ...
- day 18 C语言顺序结构基础定义1
(1).有以下程序: 程序运行后的输出结果是[B] (A).3,5,5,3 (B).3,5,3,5 (C).5,3,3,5 (D).5,3,5,3 这个题其实也可以弄成改错题,传到函数里面要对其值操作 ...
- Visaul Studio 2015 MFC 应用程序工程创建
近一段时间开始接触到MFC桌面开发程序,忙完了一段时间的项目开发之后,来整理整理Visaul Studio 2015开发MFC桌面程序的基本功能. 首先从创建软件工程项目开始,Visaul Studi ...
- 【reverse】逆向7 堆栈图
[reverse]逆向7 堆栈图 前言 本章就是开始画堆栈图来打基础拉,堆栈熟悉了之后就可以开始C语言的逆向了. 这一章使用的exe文件,我已经上传到了我的个人网盘中,点击下载 1.准备工作 先看这张 ...
- PWA 技术落地!让你的站点(Web)秒变APP(应用程序)
Web应用方兴未艾,我们已经十分习惯习惯了在电脑上进行以自己的工作,而随着众多功能强大的在线网站,我们的Windows的桌面也不再拥挤着各种快捷方式:不光是PC端,在移动端我们也不再在浩如烟海的应用市 ...