Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Puppeteer Library in Node.js

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use the Puppeteer library in Node.js". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the Puppeteer library in Node.js.

Puppeteer is an official Node library from Google that controls headless Chrome through the DevTools protocol. You can use the api provided by Puppeteer to directly control Chrome to simulate most user operations for UI Test or to access pages as crawlers to collect data.

Chinese document

Https://zhaoqize.github.io/puppeteer-api-zh_CN/#/

Function:

What can I do?

Generate the page PDF.

Grab SPA (single page application) and generate pre-rendered content.

Automatically submit forms, conduct UI tests, keyboard input, etc.

Create an automated test environment that is constantly updated. Use the latest JavaScript and browser capabilities to perform tests directly in the latest version of Chrome.

Capture the timeline trace of a website to help analyze performance problems.

Test the browser extension.

Puppeteer is easy to install because it is a npm package:

Npm i puppeteer

Or

Yarn add puppeteer

How to use:

/ / introduce Puppeteer module let puppeteer = require ('puppeteer') / / puppeteer.launch instantiate open browser async function test () {/ / you can pass in an options object ({headless: false}), which can be configured as an interface-less browser, or you can configure an interface browser / / an interface-free browser with higher and faster performance Interface is generally used to debug and develop let options = {/ / set the width and height of the window defaultViewport: {width:1400, height:800}, / / set to have an interface, if true That is, headless:false without interface, / / set the number of milliseconds to slow down each step slowMo:250} let browser = await puppeteer.launch (options) / / call the new page let page = await browser.newPage (); / / configure to visit the URL await page.goto ('http://www.baidu.com') / / screenshot await page.screenshot ({path:' test.png'}); / / print pdf await page.pdf ({path: 'example.pdf', format:' A4'}); / / close the await browser.close () } test ()

/ / get the page content / / $$eval function so that the callback function can be run in the browser and output await page.$$eval ('# head # s-top-left asides, (res) = > {/ / console.log (res); res.forEach ((item,index) = > {console.log ($(item) .attr ('href') })}) / / listen to the console.log event page.on ('console', (... args) = > {console.log (args);}) / / get the page object and add the click event ElementHandle = await page.$$ (' # head # s-top-left a') ElementHandle [0] .click ()

/ / search through form input inputBox = await page.$ ('# form .s _ ipt_wr # kw') await inputBox.focus () / / position the cursor in the input box await page.keyboard.type ('Node.js') / / enter the content into the input box search = await page.$ (' .s _ btn_wr input [type=submit]') search.click () / / Click the search button

Crawler practice

Many web pages use user-agent to determine the device, and page.emulate (options) can be used to simulate. Options has two configuration items, one is userAgent, and the other is viewport, which can set width (width), height (height), screen zoom (deviceScaleFactor), whether it is mobile (isMobile), and whether there are touch events (hasTouch).

Const puppeteer = require ('puppeteer'); const devices = require (' puppeteer/DeviceDescriptors'); const iPhone = devices ['iPhone 6']; puppeteer.launch (). Then (async browser = > {const page = await browser.newPage (); await page.emulate (iPhone); await page.goto ('https://www.example.com'); / / other actions...) Await browser.close ();}

The above code simulates iPhone6 visiting a website, where devices is a simulation parameter for some common devices built into puppeteer.

Many web pages need to log in, and there are two solutions:

Common ways to get puppeteer to enter account passwords: click to use the page.click (selector [, options]) method, or you can choose to focus on page.focus (selector). Input can use page.type (selector, text [, options]) to enter a specified string, and you can set delay slow input in options to be more like a real person. You can also use keyboard.down (key [, options]) to input one character at a time.

If the login status is judged by cookie, you can use page.setCookie (. Cookies). If you want to maintain cookie, you can access it regularly.

Tip: some websites need to scan the code, but other pages with the same domain name are logged in, so you can try to log in to the page where you can log in and skip code scanning with cookie access.

Thank you for your reading, the above is the content of "how to use the Puppeteer library in Node.js". After the study of this article, I believe you have a deeper understanding of how to use the Puppeteer library in Node.js, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report