Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
WorkerConf 2021 - Browser automation with Puppe...
Search
Jecelyn Yeen
June 25, 2021
Programming
1.9k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
WorkerConf 2021 - Browser automation with Puppeteer
Jecelyn Yeen
June 25, 2021
More Decks by Jecelyn Yeen
See All by Jecelyn Yeen
My Journey as Technical Presenter
jecfish
0
1k
Becoming a GDE
jecfish
0
3.9k
How to get started in IT Freelancing
jecfish
1
300
New Web API & Stylings
jecfish
3
760
如何打造高性能且SEO友好的单页应用(SPA)
jecfish
2
920
JS SEO
jecfish
4
1.6k
Web Performance Optimization
jecfish
5
1.8k
Have Fun with Angular
jecfish
0
130
RxJs Trex
jecfish
0
710
Other Decks in Programming
See All in Programming
継続モナドとリアクティブプログラミング
yukikurage
3
650
才能?センス?知らん、 続けたもん勝ちだ。-- 結婚・出産・癌を越えてなお、私がプロダクトを創り続ける理由
16bitidol
2
920
PHP に部分適用が来るぞ!……ところで何それ?おいしいの? #phpcon / phpcon-2026
shogogg
0
380
Welcome to the "Parametricity" 🏙️ − Generic だけど Specific な世界 −
guvalif
PRO
1
180
変わらないものが、変わるものを決める — 意図駆動開発 × イベントソーシング × イミュータブル | What Doesn't Change Decides What Can — IDD × Event Sourcing × Immutability
tomohisa
0
500
自作OSでスライド発表する
uyuki234
1
3.9k
AI駆動開発を妨げる技術的負債の解消アプローチ / ai-refactoring-approach
minodriven
17
9.4k
Generative UI & AI-Assistants for Your Angular Solutions
manfredsteyer
PRO
0
120
AI時代、エンジニアはどう育つのか -未経験エンジニアの成長を間近で見て考えたこと-
thasu0123
0
180
壊れたパーサから始める関数型設計と構成的なパーサ #fp_matsuri
raiga0310
2
400
『コードを書く以外の』エンジニアリング〜課金基盤移行プロジェクト推進のためのTips4選
yuriko1211
0
550
AWS CDK を「作」ってみた 〜フルスクラッチで見えた CDK の裏側〜 / aws-cdk-from-scratch
gotok365
3
2.6k
Featured
See All Featured
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
GitHub's CSS Performance
jonrohan
1033
470k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.7k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
260
The World Runs on Bad Software
bkeepers
PRO
72
12k
WCS-LA-2024
lcolladotor
0
760
Context Engineering - Making Every Token Count
addyosmani
9
1k
Chasing Engaging Ingredients in Design
codingconduct
0
240
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
220
Designing for Performance
lara
611
70k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.5k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
660
Transcript
@JecelynYeen Developer advocate, Google @ChromeDevTools
None
My road bike
Mountain bike My road bike
Mountain bike Hiking, hot and humid My road bike
Mountain bike Hiking, hot and humid Hiking, Switzerland My road
bike
None
Browser automation with Puppeteer
PM YANG Professional developer PAUL
PM YANG I want a daily email on our server’s
internet speed. Professional developer PAUL
PM YANG I want a daily email on our server’s
internet speed. Professional developer PAUL Why does he need that?
PM YANG It’s easy. Go to fast.com on the server
and send me the screenshot.
PM YANG It’s easy. Go to fast.com on the server
and send me the screenshot. Professional developer PAUL I never said it’s hard...
None
None
Professional developer PAUL There are 2 ways to do this:
Manually 1. Login to server 2. Open a browser 3. Go to fast.com 4. Take a screenshot 5. Send email
Professional developer PAUL There are 2 ways to do this:
Manually 1. Login to server 2. Open a browser 3. Go to fast.com 4. Take a screenshot 5. Send email Automate it 1. Schedule a daily server task 2. Automate screenshot 3. Automate email sending
Professional developer PAUL Researching...
Professional developer PAUL Researching... Puppeteer
None
Browser automation library for Node.js
Control a browser with modern JavaScript API.
const puppeteer = require('puppeteer'); (async () => { })();
const puppeteer = require('puppeteer'); (async () => { const browser
= await puppeteer.launch(); })();
const puppeteer = require('puppeteer'); (async () => { const browser
= await puppeteer.launch(); const page = await browser.newPage(); })();
const puppeteer = require('puppeteer'); (async () => { const browser
= await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://fast.com/'); })();
const puppeteer = require('puppeteer'); (async () => { const browser
= await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://fast.com/'); await page.screenshot({ path: 'img/screenshot-yyyyMMdd.png' }); })();
const puppeteer = require('puppeteer'); (async () => { const browser
= await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://fast.com/'); await page.screenshot({ path: 'img/screenshot-yyyyMMdd.png' }); await browser.close(); })();
Not working
None
const puppeteer = require('puppeteer'); (async () => { const browser
= await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://fast.com/'); await page.waitForSelector('.speed-results-container.succeeded'); await page.screenshot({ path: 'img/screenshot-yyyyMMdd.png' }); await browser.close(); })();
Run it again
Professional developer PAUL
PM YANG Cool, it will be great if to have
date in the screenshot. Possible?
const puppeteer = require('puppeteer'); (async () => { const browser
= await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://fast.com/'); await page.waitForSelector('.speed-results-container.succeeded'); await page.$eval('#your-speed-message', (el, dt) => { el.innerText = `Internet speed as of ${dt} is`; }, 'yyyy-MM-dd')); await page.screenshot({ path: `img/screenshot-yyyyMMdd.png` }); await browser.close(); })();
PM YANG I want...
// Save as pdf, only work in headless mode await
page.pdf({ path: `doc/speed-yyyyMMdd.pdf` }); // Click to show more details await page.click('#show-more-details-link'); await page.waitForSelector('#upload-label.succeeded', { timeout: 0 }); // Emulate mobile view and save screenshot // src/common/DeviceDescriptors.ts const tablet = puppeteer.devices['iPad Mini']; await page.emulate(tablet); await page.screenshot({ path: `img/screenshot-yyyyMMdd.png` }); // Reload page await page.reload();
// Save as pdf, only work in headless mode await
page.pdf({ path: `doc/speed-yyyyMMdd.pdf` }); // Click to show more details await page.click('#show-more-details-link'); await page.waitForSelector('#upload-label.succeeded', { timeout: 0 }); // Emulate mobile view and save screenshot // src/common/DeviceDescriptors.ts const tablet = puppeteer.devices['iPad Mini']; await page.emulate(tablet); await page.screenshot({ path: `img/screenshot-yyyyMMdd.png` }); // Reload page await page.reload();
// Save as pdf, only work in headless mode await
page.pdf({ path: `doc/speed-yyyyMMdd.pdf` }); // Click to show more details await page.click('#show-more-details-link'); await page.waitForSelector('#upload-label.succeeded', { timeout: 0 }); // Emulate mobile view and save screenshot // src/common/DeviceDescriptors.ts const tablet = puppeteer.devices['iPad Mini']; await page.emulate(tablet); await page.screenshot({ path: `img/screenshot-yyyyMMdd.png` }); // Reload page await page.reload();
// Save as pdf, only work in headless mode await
page.pdf({ path: `doc/speed-yyyyMMdd.pdf` }); // Click to show more details await page.click('#show-more-details-link'); await page.waitForSelector('#upload-label.succeeded', { timeout: 0 }); // Emulate mobile view and save screenshot // src/common/DeviceDescriptors.ts const tablet = puppeteer.devices['iPad Mini']; await page.emulate(tablet); await page.screenshot({ path: `img/screenshot-yyyyMMdd.png` }); // Reload page await page.reload();
github.com/puppeteer
❤
const browser = await puppeteer.launch(); const page = await browser.newPage();
await page.goto('https://v8.dev/blog/10-years'); // Create screenshots of your web app in Light Mode and Dark Mode. // Tip: check out `npx dark-mode-screenshot` for more options! await page.emulateMediaFeatures([{ name: 'prefers-color-scheme', value: 'light' }]); await page.screenshot({ path: 'light.png', fullPage: true }); await page.emulateMediaFeatures([{ name: 'prefers-color-scheme', value: 'dark' }]); await page.screenshot({ path: 'dark.png', fullPage: true }); await browser.close();
const browser = await puppeteer.launch(); const page = await browser.newPage();
await page.goto('https://mathiasbynens.be/demo/timezone'); page.evaluate(() => { globalThis.date = new Date(1479579154987); }); await page.emulateTimezone('America/Buenos_Aires'); console.log(await page.evaluate(() => date.toString())); // → 'Sat Nov 19 2016 15:12:34 GMT-0300 (Argentina Standard Time)' await page.emulateTimezone('Asia/Kuala_Lumpur'); console.log(await page.evaluate(() => date.toString())); // → 'Sun Nov 20 2016 02:12:34 GMT+0800 (Malaysia Time)' await browser.close();
const browser = await puppeteer.launch(); const page = await browser.newPage();
await page.goto('https://developer.chrome.com/tags/devtools/'); await page.emulateVisionDeficiency('blurredVision'); await page.screenshot({ path: 'blurred-vision.png', fullPage: true }); await page.emulateVisionDeficiency('achromatopsia'); await page.screenshot({ path: 'achromatopsia.png', fullPage: true }); await page.emulateVisionDeficiency('deuteranopia'); await page.screenshot({ path: 'deuteranopia.png', fullPage: true }); await browser.close();
addyosmani.com/blog/ puppeteer-recipes/
const browser = await puppeteer.launch();
const browser = await puppeteer.launch(); // Launch options const browser
= await puppeteer.launch({ product: 'firefox' });
const browser = await puppeteer.launch(); // Launch options const browser
= await puppeteer.launch({ product: 'firefox' }); const browser = await puppeteer.launch({ headless: false, slowMo: 3000 });
const browser = await puppeteer.launch(); // Launch options const browser
= await puppeteer.launch({ product: 'firefox' }); const browser = await puppeteer.launch({ headless: false, slowMo: 3000 }); const browser = await puppeteer.launch({ devtools: true });
goo.gle/devtools-blog
@JecelynYeen Developer advocate, Google @ChromeDevTools