An example introduction to the Headless Chrome development tool library
Headless Chrome refers to running Google Chrome in headless mode. The essence is to run Google without Google! It translates all the features of modern web platforms provided by the Chromium and Blink rendering engines into the command line.
what's it for?
Headless Browser is a great tool for automated testing and servers that do not require a visual user interface. For example, you want to run some tests on a web page, create a PDF from the web page, or just check how the browser submits the URL.
Warning: Chrome 59 on Mac and Linux can run in Headless mode. Windows support will be provided soon.
Run Headless Chrome from the command line
Chrome installation (requires a ladder)
Download address
Comparison of several versions
Chromium is not Chrome, but the content of Chrome basically comes from Chromium. This is an open source version, updated hourly
Canary is the experimental version, which translates to canary. Canary is very sensitive to poisonous gases such as gas. If the concentration is slightly higher, it will stop chirping or even die. Canary is a crude method for detecting gas and other poisonous gases. This scene is in It can also be seen in Huang Bo's operation in "The Secret of the Dragon". Haha, I'm going too far, this is the daily build version.
Dev is the development version, weekly build version
Beta is the test version, monthly build version
Stable is a stable version, updated from time to time, usually about once a month
Update frequencyChromium > Chrome Canary > Chrome Dev > Chrome Beta > Chrome Stable
Chrome Dev, Chrome Beta and Chrome Stable can only have one of them at the same time
Chromium, Chrome Canary and any of the remaining ones can coexist
The Windows platform download may only be an online installation program. To download the offline version, add parameters to the URL of the download page
standalone=1
Command line quick configuration (Mac environment)
Add <pre class="brush:php;toolbar:false">alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
alias chrome-canary="/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary"</pre>
to
Reopen the terminal, we can directly passchrome
Open the stable version of Chrome, chrome-canary
Open the experimental version of Chrome.
Start Chrome from the command line
Refer to the official instructions, Headless mode requires Chrome Version >= 59
Use Chrome to open Baidu homepage (with interface), you can see the browser opening
chrome
Start in interface-less mode Chrome opens the Baidu homepage (no interface), but does not open the browser interface, but there is an icon on the taskbar
chrome --headless
Start Chrome in headless mode and convert the page to PDF. You can see the output of
output.pdf
chrome --headless --print-to-pdf
Start Chrome in headless mode and take a screenshot. You can see the output of
screenshot.png
chrome --headless --screenshot --window-size=414,736
Launch Chrome using headless mode and open Interactive environment
chrome --headless --repl
Start Chrome in headless mode and turn on the debugging server
chrome --headless --remote-debugging-port=9222
- ##Reference Chrome command line parameter list
- Make sure Headless Chrome is started and debugging Server is enabled
chrome --headless --remote-debugging-port=9222
- ##Installation
- chrome-remote-interface
- View the command description, various related operations can be performed here
- "
Usage: chrome-remote-interface [options] [command] Commands: inspect [options] [<target>] inspect a target (defaults to the first available target) list list all the available targets/tabs new [<url>] create a new target/tab activate <id> activate a target/tab by id close <id> close a target/tab by id version show the browser version protocol [options] show the currently available protocol descriptor Options: -h, --help output usage information -t, --host <host> HTTP frontend host -p, --port <port> HTTP frontend port -s, --secure HTTPS/WSS frontend</port></host></id></id></url></target>
"
- Open a new page
- View the page you just opened
- View the URL of the current page
- ##>>> Runtime.evaluate({expression:' location.href'})
Run Headless Chrome programmatically
Google出品的Lighthouse 这个网页质量检查工具,有一个组件专门做这事,考虑了各种平台的兼容性问题,源码参考lighthouse-chromelauncher,这个组件现在已经单独独立出来,作为一个单独的NPM
组件chrome-launcher
,可以直接使用这个在Node
平台下调用,其他平台的也可以此为参考。
const chromeLauncher = require('chrome-launcher');//启用无界面模式并开启远程调试,不同引用版本和方式,调用方式可能有些区别//chromeLauncher.run({chromeLauncher.launch({// port: 9222,chromeFlags: ['--headless']}).then((chrome) => {// 拿到一个调试客户端实例console.log(chrome)chrome.kill();});
通过客户端的封装组件进行浏览器交互
实现了ChromeDevTools
协议的工具库有很多,chrome-remote-interface
是NodeJS的实现。
Chrome调试Server开启的是WebSocket交互的相关实现,要用编程的方式实现还需要封装一些WebSocket命令发送、结果接收等这一系列操作,这些chrome-remote-interface
已经帮我们做了,更多实例可以参考chrome-remote-interface的wiki。
const chromeLauncher = require('chrome-launcher');const chromeRemoteInterface = require('chrome-remote-interface')//启用无界面模式并开启远程调试,不同引用版本和方式,调用方式可能有些区别//chromeLauncher.run({chromeLauncher.launch({port: 9222,chromeFlags: ['--headless']}).then((launcher) => {chromeRemoteInterface.Version({host:'localhost',port:9222}).then(versionInfo => {console.log(versionInfo)});chromeRemoteInterface({host:'localhost',port:9222}).then((chrome) => {//这里调用ChromeDevToolsProtocol定义的接口const {Network,Page} = chrome;Network.requestWillBeSent((params) => {let {request} = params;let {url} = request;console.log(url)});Promise.all([Network.enable(),Page.enable() ]).then(() => {Page.navigate({url:'https://www.baidu.com'})});setTimeout(() => {launcher.kill()},5000);})});
The above is the detailed content of An example introduction to the Headless Chrome development tool library. For more information, please follow other related articles on the PHP Chinese website!

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor