您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
三六零分类信息网 > 连云港分类信息网,免费分类信息发布

Headless Chrome开发工具库的实例介绍

2025/1/19 2:49:49发布51次查看
headless chrome指在headless模式下运行谷歌浏览器。本质就是不用谷歌运行谷歌!它将由chromium和blink渲染引擎提供的所有现代网页平台的特征都转化成了命令行。
它有什么用?
headless浏览器是一种很好的工具,用于自动化测试和不需要可视化用户界面的服务器。例如,你想在一个网页上运行一些测试,从网页创建一个pdf,或者只是检查浏览器怎样递交url。
警告:在mac和linux上的chrome 59可以运行headless模式。支持windows的会很快提供。
命令行运行headless chromechrome 安装(需要带梯子)下载地址
几个版本的比较
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
命令行快捷配置(mac环境)在~/.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了。
命令行启动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确保已经启动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] [b4bef09dd2761803871f1d83e55d08b2] inspect a target (defaults to the first available target)   list                   list all the available targets/tabs   new [9bb6a7d109b3f2bf35f7e2e9bd87f98a]            create a new target/tab   activate 53384f78b45ee9f1e3082cf378b9c5b4          activate a target/tab by id   close 53384f78b45ee9f1e3082cf378b9c5b4             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 f7e6dec31ab1a0471d06c55afaca8d77  http frontend host   -p, --port 298c9bd6ad6e8c821dc63aa0473d6209  http frontend port   -s, --secure       https/wss frontend
打开一个新页面
chrome-remote-interface new
查看刚打开的页面
chrome-remote-interface inspect
查看当前页面的url
>>> runtime.evaluate({expression:'location.href'})
可编程方式运行headless chrome直接通过代码调用命令行启动chrome 调试server可以通过系统调用的方式直接调用上面的命令行执行方式。这种方式在跨平台的情况下会有一些工作需要做。
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);})});
以上就是headless chrome开发工具库的实例介绍的详细内容。
连云港分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录