SlideShare una empresa de Scribd logo
1 de 16
Descargar para leer sin conexión
Puppeteer
Headless Chrome Node API
Wilson Su
What Is a Headless Browser?
A headless browser is a web browser without
a graphical user interface. Headless browsers
provide automated control of a web page in
an environment similar to popular web
browsers, but are executed via a
command-line interface or using network
communication. – Wiki
Headless Browser Command Line Features
1. // Taking screenshots
2. $ google-chrome --headless --disable-gpu --screenshot
https://example.com/
3.
4. // Creating a PDF
5. $ google-chrome --headless --disable-gpu --print-to-pdf
https://example.com/
Puppeteer
https://github.com/GoogleChrome/puppeteer
Use Cases
✓ Take screenshots
✓ Generate PDFs
✓ Scrape content
✓ Automate UI testing
✓ Capture a timeline trace
✗ DDoS attack
✗ Fake page loads
✗ Credential stuffing
Examples
1. Take screenshosts
2. Generate PDFs
3. Automate interactions
4. Scrape content from websites
5. Emulate device metrics and user agent
6. Handle events
7. Create trace files
Taking Screenshots
1. const puppeteer = require('puppeteer');
2. (async () => {
3. const browser = await puppeteer.launch();
4. const page = await browser.newPage();
5. await page.setViewport({ width: 767, height: 1024 });
6. await page.goto('https://getbootstrap.com/');
7. await page.screenshot({
8. path: 'bootstrap.png',
9. fullPage: true
10. });
11. browser.close();
12. })();
Generating PDFs
1. const puppeteer = require('puppeteer');
2. (async () => {
3. const browser = await puppeteer.launch();
4. const page = await browser.newPage();
5. await page.goto('https://github.com/');
6. await page.pdf({ path: 'github.pdf', format: 'A4' });
7. browser.close();
8. })();
Automating Interactions
1. const puppeteer = require('puppeteer');
2. (async () => {
3. const browser = await puppeteer.launch();
4. const page = await browser.newPage();
5. await page.goto('https://www.npmjs.com');
6. await page.focus('#site-search');
7. await page.type('react');
8. await page.click('#npm-search > button');
9. await page.waitForNavigation();
10. await page.screenshot({ path: 'npmjs.png' });
11. browser.close();
12. })();
Scraping Content From Websites
1. const puppeteer = require('puppeteer');
2. (async () => {
3. const browser = await puppeteer.launch();
4. const page = await browser.newPage();
5. await page.goto('https://www.npmjs.com/search?q=react');
6. const packages = await page.evaluate(() => {
7. var nodes = document.querySelectorAll('.packageName');
8. return [...nodes].map(el => el.textContent);
9. });
10. console.log(packages);
11. browser.close();
12. })();
Emulating Device Metrics and User Agent
1. const puppeteer = require('puppeteer');
2. const devices = require('puppeteer/DeviceDescriptors');
3. const iphone6 = devices['iPhone 6'];
4. (async () => {
5. const browser = await puppeteer.launch();
6. const page = await browser.newPage();
7. await page.emulate(iphone6);
8. await page.goto('https://www.facebook.com/');
9. await page.screenshot({ path: 'facebook.png' });
10. const host = await page.evaluate(() => location.host);
11. console.log(host); // 'm.facebook.com'
12. browser.close();
13. })();
Handling Events
1. const puppeteer = require('puppeteer');
2. (async () => {
3. const browser = await puppeteer.launch();
4. const page = await browser.newPage();
5. page.on('console', (...args) => {
6. console.log('[Browser]', ...args);
7. });
8. await page.goto('http://jsbin.com');
9. browser.close();
10. })();
Creating Trace Files
1. const puppeteer = require('puppeteer');
2. (async () => {
3. const browser = await puppeteer.launch();
4. const page = await browser.newPage();
5. await page.tracing.start({ path: 'trace.json' });
6. await page.goto('https://webpack.js.org/', {
7. waitUntil: 'networkidle'
8. });
9. await page.tracing.stop();
10. browser.close();
11. })();
Get Samples
https://github.com/sweekson/puppeteer-samples
Further Information
● Selenium - Web Browser Automation
● WebdriverIO - WebDriver bindings for Node.js
● PhantomJS
● SlimerJS
● Chromeless
Reference
● Headless browser - Wikipedia
● Getting Started with Headless Chrome  |  Web  |  Google Developers
● What Is a Headless Browser, and What's It Good For?
● Jack Histon - Making a Master Puppeteer

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Playwright: A New Test Automation Framework for the Modern Web
Playwright: A New Test Automation Framework for the Modern WebPlaywright: A New Test Automation Framework for the Modern Web
Playwright: A New Test Automation Framework for the Modern Web
 
Introducing Playwright's New Test Runner
Introducing Playwright's New Test RunnerIntroducing Playwright's New Test Runner
Introducing Playwright's New Test Runner
 
20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native20180518 QNAP Seminar - Introduction to React Native
20180518 QNAP Seminar - Introduction to React Native
 
Drive chrome(headless) with puppeteer
Drive chrome(headless) with puppeteerDrive chrome(headless) with puppeteer
Drive chrome(headless) with puppeteer
 
Cypress-vs-Playwright: Let the Code Speak
Cypress-vs-Playwright: Let the Code SpeakCypress-vs-Playwright: Let the Code Speak
Cypress-vs-Playwright: Let the Code Speak
 
Building beautiful apps using google flutter
Building beautiful apps using google flutterBuilding beautiful apps using google flutter
Building beautiful apps using google flutter
 
jQuery Ajax
jQuery AjaxjQuery Ajax
jQuery Ajax
 
Introduction to Flutter - truly crossplatform, amazingly fast
Introduction to Flutter - truly crossplatform, amazingly fastIntroduction to Flutter - truly crossplatform, amazingly fast
Introduction to Flutter - truly crossplatform, amazingly fast
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
 
Flutter
FlutterFlutter
Flutter
 
Cypress-vs-Playwright-Rematch-Applitools.pdf
Cypress-vs-Playwright-Rematch-Applitools.pdfCypress-vs-Playwright-Rematch-Applitools.pdf
Cypress-vs-Playwright-Rematch-Applitools.pdf
 
Hello Flutter
Hello FlutterHello Flutter
Hello Flutter
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node js
 
Building beautiful apps with Google flutter
Building beautiful apps with Google flutterBuilding beautiful apps with Google flutter
Building beautiful apps with Google flutter
 
The redux saga begins
The redux saga beginsThe redux saga begins
The redux saga begins
 
Angular - Chapter 3 - Components
Angular - Chapter 3 - ComponentsAngular - Chapter 3 - Components
Angular - Chapter 3 - Components
 
Build RESTful API Using Express JS
Build RESTful API Using Express JSBuild RESTful API Using Express JS
Build RESTful API Using Express JS
 
Playwright Begginers Presentation
Playwright Begginers PresentationPlaywright Begginers Presentation
Playwright Begginers Presentation
 
Asynchronous javascript
 Asynchronous javascript Asynchronous javascript
Asynchronous javascript
 

Similar a Puppeteer - Headless Chrome Node API

BlackBerry DevCon 2011 - PhoneGap and WebWorks
BlackBerry DevCon 2011 - PhoneGap and WebWorksBlackBerry DevCon 2011 - PhoneGap and WebWorks
BlackBerry DevCon 2011 - PhoneGap and WebWorks
mwbrooks
 
Native apps in html5 with chrome packaged apps
Native apps in html5 with chrome packaged appsNative apps in html5 with chrome packaged apps
Native apps in html5 with chrome packaged apps
Tom Wilson
 
[HEWEBAR 2012] Adaptive Images in Responsive Web Design
[HEWEBAR 2012] Adaptive Images in Responsive Web Design[HEWEBAR 2012] Adaptive Images in Responsive Web Design
[HEWEBAR 2012] Adaptive Images in Responsive Web Design
Christopher Schmitt
 

Similar a Puppeteer - Headless Chrome Node API (20)

vodQA Pune (2019) - Browser automation using dev tools
vodQA Pune (2019) - Browser automation using dev toolsvodQA Pune (2019) - Browser automation using dev tools
vodQA Pune (2019) - Browser automation using dev tools
 
Puppeteer can automate that! - HolyJS Piter 2020
Puppeteer can automate that! - HolyJS Piter 2020Puppeteer can automate that! - HolyJS Piter 2020
Puppeteer can automate that! - HolyJS Piter 2020
 
Detecting headless browsers
Detecting headless browsersDetecting headless browsers
Detecting headless browsers
 
BlackBerry DevCon 2011 - PhoneGap and WebWorks
BlackBerry DevCon 2011 - PhoneGap and WebWorksBlackBerry DevCon 2011 - PhoneGap and WebWorks
BlackBerry DevCon 2011 - PhoneGap and WebWorks
 
FINHTML5 - Breaking the mobile web
FINHTML5 - Breaking the mobile webFINHTML5 - Breaking the mobile web
FINHTML5 - Breaking the mobile web
 
Android UI Testing with Appium
Android UI Testing with AppiumAndroid UI Testing with Appium
Android UI Testing with Appium
 
Complete_QA_Automation_Guide__1696637878.pdf
Complete_QA_Automation_Guide__1696637878.pdfComplete_QA_Automation_Guide__1696637878.pdf
Complete_QA_Automation_Guide__1696637878.pdf
 
Velocity EU 2012 - Third party scripts and you
Velocity EU 2012 - Third party scripts and youVelocity EU 2012 - Third party scripts and you
Velocity EU 2012 - Third party scripts and you
 
ДМИТРО БУДИМ «Mobile Automation Infrastructure from scratch» Online QADay 202...
ДМИТРО БУДИМ «Mobile Automation Infrastructure from scratch» Online QADay 202...ДМИТРО БУДИМ «Mobile Automation Infrastructure from scratch» Online QADay 202...
ДМИТРО БУДИМ «Mobile Automation Infrastructure from scratch» Online QADay 202...
 
High Performance Snippets
High Performance SnippetsHigh Performance Snippets
High Performance Snippets
 
Native apps in html5 with chrome packaged apps
Native apps in html5 with chrome packaged appsNative apps in html5 with chrome packaged apps
Native apps in html5 with chrome packaged apps
 
Selenium Commands (Short Interview Preparation)
Selenium Commands (Short Interview Preparation)Selenium Commands (Short Interview Preparation)
Selenium Commands (Short Interview Preparation)
 
GDG İstanbul Şubat Etkinliği - Sunum
GDG İstanbul Şubat Etkinliği - SunumGDG İstanbul Şubat Etkinliği - Sunum
GDG İstanbul Şubat Etkinliği - Sunum
 
Web Applications with AngularJS
Web Applications with AngularJSWeb Applications with AngularJS
Web Applications with AngularJS
 
Sanjeev ghai 12
Sanjeev ghai 12Sanjeev ghai 12
Sanjeev ghai 12
 
[HEWEBAR 2012] Adaptive Images in Responsive Web Design
[HEWEBAR 2012] Adaptive Images in Responsive Web Design[HEWEBAR 2012] Adaptive Images in Responsive Web Design
[HEWEBAR 2012] Adaptive Images in Responsive Web Design
 
Building high performance web apps.
Building high performance web apps.Building high performance web apps.
Building high performance web apps.
 
Google I/O 2012 - Protecting your user experience while integrating 3rd party...
Google I/O 2012 - Protecting your user experience while integrating 3rd party...Google I/O 2012 - Protecting your user experience while integrating 3rd party...
Google I/O 2012 - Protecting your user experience while integrating 3rd party...
 
Play framework
Play frameworkPlay framework
Play framework
 
MeasureCamp IX (London) - 10 JavaScript Concepts for web analysts
MeasureCamp IX (London) - 10 JavaScript Concepts for web analystsMeasureCamp IX (London) - 10 JavaScript Concepts for web analysts
MeasureCamp IX (London) - 10 JavaScript Concepts for web analysts
 

Más de Wilson Su

Más de Wilson Su (13)

Mirage For Beginners
Mirage For BeginnersMirage For Beginners
Mirage For Beginners
 
NestJS
NestJSNestJS
NestJS
 
The Jira How-To Guide
The Jira How-To GuideThe Jira How-To Guide
The Jira How-To Guide
 
The Future of Web Development
The Future of Web DevelopmentThe Future of Web Development
The Future of Web Development
 
Web Usability
Web UsabilityWeb Usability
Web Usability
 
Practical JavaScript Programming - Session 8/8
Practical JavaScript Programming - Session 8/8Practical JavaScript Programming - Session 8/8
Practical JavaScript Programming - Session 8/8
 
Practical JavaScript Programming - Session 7/8
Practical JavaScript Programming - Session 7/8Practical JavaScript Programming - Session 7/8
Practical JavaScript Programming - Session 7/8
 
Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8
 
Practical JavaScript Programming - Session 5/8
Practical JavaScript Programming - Session 5/8Practical JavaScript Programming - Session 5/8
Practical JavaScript Programming - Session 5/8
 
Practical JavaScript Programming - Session 4/8
Practical JavaScript Programming - Session 4/8Practical JavaScript Programming - Session 4/8
Practical JavaScript Programming - Session 4/8
 
Practical JavaScript Programming - Session 3/8
Practical JavaScript Programming - Session 3/8Practical JavaScript Programming - Session 3/8
Practical JavaScript Programming - Session 3/8
 
Practical JavaScript Programming - Session 2/8
Practical JavaScript Programming - Session 2/8Practical JavaScript Programming - Session 2/8
Practical JavaScript Programming - Session 2/8
 
Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8Practical JavaScript Programming - Session 1/8
Practical JavaScript Programming - Session 1/8
 

Último

VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 

Último (20)

VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 

Puppeteer - Headless Chrome Node API

  • 2. What Is a Headless Browser? A headless browser is a web browser without a graphical user interface. Headless browsers provide automated control of a web page in an environment similar to popular web browsers, but are executed via a command-line interface or using network communication. – Wiki
  • 3. Headless Browser Command Line Features 1. // Taking screenshots 2. $ google-chrome --headless --disable-gpu --screenshot https://example.com/ 3. 4. // Creating a PDF 5. $ google-chrome --headless --disable-gpu --print-to-pdf https://example.com/
  • 5. Use Cases ✓ Take screenshots ✓ Generate PDFs ✓ Scrape content ✓ Automate UI testing ✓ Capture a timeline trace ✗ DDoS attack ✗ Fake page loads ✗ Credential stuffing
  • 6. Examples 1. Take screenshosts 2. Generate PDFs 3. Automate interactions 4. Scrape content from websites 5. Emulate device metrics and user agent 6. Handle events 7. Create trace files
  • 7. Taking Screenshots 1. const puppeteer = require('puppeteer'); 2. (async () => { 3. const browser = await puppeteer.launch(); 4. const page = await browser.newPage(); 5. await page.setViewport({ width: 767, height: 1024 }); 6. await page.goto('https://getbootstrap.com/'); 7. await page.screenshot({ 8. path: 'bootstrap.png', 9. fullPage: true 10. }); 11. browser.close(); 12. })();
  • 8. Generating PDFs 1. const puppeteer = require('puppeteer'); 2. (async () => { 3. const browser = await puppeteer.launch(); 4. const page = await browser.newPage(); 5. await page.goto('https://github.com/'); 6. await page.pdf({ path: 'github.pdf', format: 'A4' }); 7. browser.close(); 8. })();
  • 9. Automating Interactions 1. const puppeteer = require('puppeteer'); 2. (async () => { 3. const browser = await puppeteer.launch(); 4. const page = await browser.newPage(); 5. await page.goto('https://www.npmjs.com'); 6. await page.focus('#site-search'); 7. await page.type('react'); 8. await page.click('#npm-search > button'); 9. await page.waitForNavigation(); 10. await page.screenshot({ path: 'npmjs.png' }); 11. browser.close(); 12. })();
  • 10. Scraping Content From Websites 1. const puppeteer = require('puppeteer'); 2. (async () => { 3. const browser = await puppeteer.launch(); 4. const page = await browser.newPage(); 5. await page.goto('https://www.npmjs.com/search?q=react'); 6. const packages = await page.evaluate(() => { 7. var nodes = document.querySelectorAll('.packageName'); 8. return [...nodes].map(el => el.textContent); 9. }); 10. console.log(packages); 11. browser.close(); 12. })();
  • 11. Emulating Device Metrics and User Agent 1. const puppeteer = require('puppeteer'); 2. const devices = require('puppeteer/DeviceDescriptors'); 3. const iphone6 = devices['iPhone 6']; 4. (async () => { 5. const browser = await puppeteer.launch(); 6. const page = await browser.newPage(); 7. await page.emulate(iphone6); 8. await page.goto('https://www.facebook.com/'); 9. await page.screenshot({ path: 'facebook.png' }); 10. const host = await page.evaluate(() => location.host); 11. console.log(host); // 'm.facebook.com' 12. browser.close(); 13. })();
  • 12. Handling Events 1. const puppeteer = require('puppeteer'); 2. (async () => { 3. const browser = await puppeteer.launch(); 4. const page = await browser.newPage(); 5. page.on('console', (...args) => { 6. console.log('[Browser]', ...args); 7. }); 8. await page.goto('http://jsbin.com'); 9. browser.close(); 10. })();
  • 13. Creating Trace Files 1. const puppeteer = require('puppeteer'); 2. (async () => { 3. const browser = await puppeteer.launch(); 4. const page = await browser.newPage(); 5. await page.tracing.start({ path: 'trace.json' }); 6. await page.goto('https://webpack.js.org/', { 7. waitUntil: 'networkidle' 8. }); 9. await page.tracing.stop(); 10. browser.close(); 11. })();
  • 15. Further Information ● Selenium - Web Browser Automation ● WebdriverIO - WebDriver bindings for Node.js ● PhantomJS ● SlimerJS ● Chromeless
  • 16. Reference ● Headless browser - Wikipedia ● Getting Started with Headless Chrome  |  Web  |  Google Developers ● What Is a Headless Browser, and What's It Good For? ● Jack Histon - Making a Master Puppeteer