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
琵琶湖の水は止められてもNet--HTTPのリトライは止められない / You might be able to stop the water flow of Lake Biwa but you can't stop Net::HTTP retries
luccafort
PRO
0
450
ルールを書いて終わらせないハーネスエンジニアリング
yug1224
4
1.8k
AI時代のPHPer生存戦略 ~「言語、もうなんでもよくない?」に本気で向き合う~
vivion
0
180
改善しないと、タスクが回らない。 “てんこ盛りポジション” を引き継いだ情シスの、入社3ヶ月の業務改善録
krm963
0
200
php-fpmのプロセスが枯渇した日-調査・対処・そして本当にやるべきだったこと-
shibuchaaaan
0
150
「正の参照」と 「負の導出」で組む ハーネスエンジニアリング
cottpan
1
150
Generative UI & AI-Assistants for Your Angular Solutions
manfredsteyer
PRO
0
120
【SRE NEXT 2026 Lunch Session】一人目専任SREの立ち上げを加速する ― AIと進めたオンボーディングで2分を0.04秒にした話
pkshadeck
PRO
0
3.1k
自作OSでスライド発表する
uyuki234
1
3.9k
生成AI導入の「期待外れ」を乗り越える ー 開発フロー改革が目指す、真の組織変革
starfish719
0
2.4k
そこに3びきプロダクトがいるじゃろう——生成AI時代における“価値が届かない理由”の構造
kosuket
0
140
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
3
660
Featured
See All Featured
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
350
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.7k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
980
We Are The Robots
honzajavorek
0
280
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Optimizing for Happiness
mojombo
378
71k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
190
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.4k
A Modern Web Designer's Workflow
chriscoyier
698
190k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
430
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
470
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