Table of Contents
Run Headless Chrome from the command line
Chrome installation (requires a ladder)
Command line quick configuration (Mac environment)
Start Chrome from the command line
通过客户端的封装组件进行浏览器交互
Home Web Front-end JS Tutorial An example introduction to the Headless Chrome development tool library

An example introduction to the Headless Chrome development tool library

Jul 18, 2017 pm 05:46 PM
chrome sass

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 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">alias chrome=&quot;/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome&quot; alias chrome-canary=&quot;/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary&quot;</pre><div class="contentsignin">Copy after login</div></div> to

~/.bashrc

Reopen the terminal, we can directly passchromeOpen the stable version of Chrome, chrome-canaryOpen 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

Command line operation Headless Chrome

  • Make sure Headless Chrome is started and debugging Server is enabled

chrome --headless --remote-debugging-port=9222

    ##Installation
  • chrome-remote-interface

npm install chrome-remote-interface -g

    View the command description, various related operations can be performed here
  • "
$ chrome-remote-interface

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
Copy after login

"

    Open a new page
chrome-remote-interface new

    View the page you just opened
chrome-remote-interface inspect

    View the URL of the current page
  • ##>>> Runtime.evaluate({expression:' location.href'})

Run Headless Chrome programmatically

Start Chrome debugging server directly by calling the command line through code

The above command line execution method can be directly called through system call. This method will have some work to do in a cross-platform situation.

Google出品的Lighthouse 这个网页质量检查工具,有一个组件专门做这事,考虑了各种平台的兼容性问题,源码参考lighthouse-chromelauncher,这个组件现在已经单独独立出来,作为一个单独的NPM组件chrome-launcher,可以直接使用这个在Node平台下调用,其他平台的也可以此为参考。

const chromeLauncher = require(&#39;chrome-launcher&#39;);//启用无界面模式并开启远程调试,不同引用版本和方式,调用方式可能有些区别//chromeLauncher.run({chromeLauncher.launch({// port: 9222,chromeFlags: [&#39;--headless&#39;]}).then((chrome) => {// 拿到一个调试客户端实例console.log(chrome)chrome.kill();});
Copy after login

通过客户端的封装组件进行浏览器交互

实现了ChromeDevTools协议的工具库有很多,chrome-remote-interface是NodeJS的实现。

Chrome调试Server开启的是WebSocket交互的相关实现,要用编程的方式实现还需要封装一些WebSocket命令发送、结果接收等这一系列操作,这些chrome-remote-interface已经帮我们做了,更多实例可以参考chrome-remote-interface的wiki。

const chromeLauncher = require(&#39;chrome-launcher&#39;);const chromeRemoteInterface = require(&#39;chrome-remote-interface&#39;)//启用无界面模式并开启远程调试,不同引用版本和方式,调用方式可能有些区别//chromeLauncher.run({chromeLauncher.launch({port: 9222,chromeFlags: [&#39;--headless&#39;]}).then((launcher) => {chromeRemoteInterface.Version({host:&#39;localhost&#39;,port:9222}).then(versionInfo => {console.log(versionInfo)});chromeRemoteInterface({host:&#39;localhost&#39;,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:&#39;https://www.baidu.com&#39;})});setTimeout(() => {launcher.kill()},5000);})});
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is Updater.exe in Windows 11/10? Is this the Chrome process? What is Updater.exe in Windows 11/10? Is this the Chrome process? Mar 21, 2024 pm 05:36 PM

Every application you run on Windows has a component program to update it. So if you are using Google Chrome or Google Earth, it will run a GoogleUpdate.exe application, check if an update is available, and then update it based on the settings. However, if you no longer see it and instead see a process updater.exe in the Task Manager of Windows 11/10, there is a reason for this. What is Updater.exe in Windows 11/10? Google has rolled out updates for all its apps like Google Earth, Google Drive, Chrome, etc. This update brings

What file is crdownload? What file is crdownload? Mar 08, 2023 am 11:38 AM

crdownload is a chrome browser download cache file, which is a file that has not been downloaded; crdownload file is a temporary file format used to store files downloaded from the hard disk. It can help users protect file integrity when downloading files and avoid being damaged. Unexpected interruption or stoppage. CRDownload files can also be used to back up files, allowing users to save temporary copies of files; if an unexpected error occurs during downloading, CRDownload files can be used to restore downloaded files.

What to do if chrome cannot load plugins What to do if chrome cannot load plugins Nov 06, 2023 pm 02:22 PM

Chrome's inability to load plug-ins can be solved by checking whether the plug-in is installed correctly, disabling and enabling the plug-in, clearing the plug-in cache, updating the browser and plug-ins, checking the network connection, and trying to load the plug-in in incognito mode. The solution is as follows: 1. Check whether the plug-in has been installed correctly and reinstall it; 2. Disable and enable the plug-in, click the Disable button, and then click the Enable button again; 3. Clear the plug-in cache, select Advanced Options > Clear Browsing Data, check cache images and files and clear all cookies, click Clear Data.

What is the Chrome plug-in extension installation directory? What is the Chrome plug-in extension installation directory? Mar 08, 2024 am 08:55 AM

What is the Chrome plug-in extension installation directory? Under normal circumstances, the default installation directory of Chrome plug-in extensions is as follows: 1. The default installation directory location of chrome plug-ins in windowsxp: C:\DocumentsandSettings\username\LocalSettings\ApplicationData\Google\Chrome\UserData\Default\Extensions2. chrome in windows7 The default installation directory location of the plug-in: C:\Users\username\AppData\Local\Google\Chrome\User

How to solve the problem that Google Chrome cannot open web pages How to solve the problem that Google Chrome cannot open web pages Jan 04, 2024 pm 10:18 PM

What should I do if the Google Chrome web page cannot be opened? Many friends like to use Google Chrome. Of course, some friends find that they cannot open web pages normally or the web pages open very slowly during use. So what should you do if you encounter this situation? Let’s take a look at the solution to the problem that Google Chrome web pages cannot be opened with the editor. Solution to the problem that the Google Chrome webpage cannot be opened. Method 1. In order to help players who have not passed the level yet, let us learn about the specific methods of solving the puzzle. First, right-click the network icon in the lower right corner and select "Network and Internet Settings." 2. Click "Ethernet" and then click "Change Adapter Options". 3. Click the "Properties" button. 4. Double-click to open i

what does chrome mean what does chrome mean Aug 07, 2023 pm 01:18 PM

Chrome means browser, a web browser developed by Google. It was first released in 2008 and quickly became one of the most popular browsers in the world. Its name comes from the browser's interface design because of its iconic The feature is the tab bar at the top of the window, and the appearance of this tab bar is very similar to chrome metal.

How to search for text across all tabs in Chrome and Edge How to search for text across all tabs in Chrome and Edge Feb 19, 2024 am 11:30 AM

This tutorial shows you how to find specific text or phrases on all open tabs in Chrome or Edge on Windows. Is there a way to do a text search on all open tabs in Chrome? Yes, you can use a free external web extension in Chrome to perform text searches on all open tabs without having to switch tabs manually. Some extensions like TabSearch and Ctrl-FPlus can help you achieve this easily. How to search text across all tabs in Google Chrome? Ctrl-FPlus is a free extension that makes it easy for users to search for a specific word, phrase or text across all tabs of their browser window. This expansion

What software is chromesetup? What software is chromesetup? Mar 03, 2023 pm 02:58 PM

chromesetup is a Google browser installation program; Google Chrome is a simple and efficient web browsing tool developed by Google. It is characterized by simplicity and speed. Chrome supports multi-tab browsing, and each tab page is in Running in an independent "sandbox" improves security, and the crash of one tab page will not cause other tab pages to be closed.

See all articles