登录  /  注册
首页 > web前端 > js教程 > 正文

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

零下一度
发布: 2017-07-18 17:46:55
原创
2203人浏览过

headless chrome指在headless模式下运行谷歌浏览器。本质就是不用谷歌运行谷歌!它将由chromium和blink渲染引擎提供的所有现代网页平台的特征都转化成了命令行。

它有什么用?

Headless浏览器是一种很好的工具,用于自动化测试和不需要可视化用户界面的服务器。例如,你想在一个网页上运行一些测试,从网页创建一个PDF,或者只是检查浏览器怎样递交URL。

警告:在Mac和Linux上的Chrome 59可以运行Headless模式。支持Windows的会很快提供。

命令行运行Headless Chrome

Chrome 安装(需要带梯子)

  • 下载地址

  • 几个版本的比较

  • 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] [<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>
登录后复制

"

  • 打开一个新页面

chrome-remote-interface new

  • 查看刚打开的页面

chrome-remote-interface inspect

  • 查看当前页面的URL

&gt;&gt;&gt; Runtime.evaluate({expression:'location.href'})

可编程方式运行Headless Chrome

直接通过代码调用命令行启动Chrome 调试Server

可以通过系统调用的方式直接调用上面的命令行执行方式。这种方式在跨平台的情况下会有一些工作需要做。

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();});
登录后复制

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

实现了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);})});
登录后复制

以上就是Headless Chrome开发工具库的实例介绍的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号