它有什么用?
Headless瀏覽器是一種很好的工具,用于自動化測試和不需要可視化用戶界面的服務器。例如,你想在一個網頁上運行一些測試,從網頁創建一個PDF,或者只是檢查瀏覽器怎樣遞交URL。
警告:在Mac和Linux上的Chrome 59可以運行Headless模式。支持Windows的會很快提供。
下載地址
幾個版本的比較
Chromium 不是Chrome,但Chrome的內容基本來源于Chromium,這個是開源的版本,小時級別的更新
Canary 是試驗版,翻譯過來就是金絲雀,金絲雀對瓦斯等毒氣很敏感,濃度稍高就會停止鳴叫甚至掛掉,金絲雀是瓦斯等毒氣檢測的土辦法,這個場景在《尋龍訣》中黃渤的操作中也能看到。哈哈 扯遠了,這個是daily build 版本。
Dev 是開發版,weekly build版本
Beta 是測試版,monthly build版本
Stable 是穩定版,不定期更新,一般也是一個月左右一次
更新頻率 Chromium > Chrome Canary > Chrome Dev > Chrome Beta > Chrome Stable
Chrome Dev、Chrome Beta 和 Chrome Stable三者只能同時出現一個
Chromium 、Chrome Canary 和 剩下的任意一個可共存
Windows平臺下載下來的可能只是一個在線安裝的程序,下載離線版在下載頁面的URL里面加參數standalone=1
在~/.bashrc
中加入
alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome" alias chrome-canary="/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary"
重新打開終端,我們就可以直接通過 chrome
打開穩定版的Chrome,chrome-canary
打開試驗版的Chrome了。
參考官方說明, Headless模式需要Chrome Version >= 59
使用Chrome打開百度首頁(帶界面),能看到瀏覽器的打開
chrome
使用無界面模式啟動Chrome打開百度首頁(無界面),但不到瀏覽器界面打開,但任務欄會有圖標
chrome --headless
使用無界面模式啟動Chrome并將頁面轉為PDF,可以看到output.pdf
的輸出
chrome --headless --print-to-pdf
使用無界面模式啟動Chrome并截圖,可以看到screenshot.png
的輸出
chrome --headless --screenshot --window-size=414,736
使用無界面模式啟動Chrome并打開交互環境
chrome --headless --repl
使用無界面模式啟動Chrome,并開啟調試Server
chrome --headless --remote-debugging-port=9222
參考 Chrome命令行參數列表
確保已經啟動Headless Chrome,并啟用了調試Server
chrome --headless --remote-debugging-port=9222
安裝chrome-remote-interface
npm install chrome-remote-interface -g
查看命令說明,這里可以進行各種相關操作
"
$ 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
"
打開一個新頁面
chrome-remote-interface new
查看剛打開的頁面
chrome-remote-interface inspect
查看當前頁面的URL
>>> Runtime.evaluate({expression:'location.href'})
可以通過系統調用的方式直接調用上面的命令行執行方式。這種方式在跨平臺的情況下會有一些工作需要做。
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);})});
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com