注:文章聚合了如今 headless chrome 介绍和使用方式css
包含了三个部分node
chrome 在 mac 上的安装和简单使用(来自官方)python
利用 selenium 的 webdrive 驱动 headless chrome(本身添加)git
利用Xvfb方式实现伪 headless chromegithub
Headless模式解决了什么问题: 自动化工具例如 selenium 利用有头浏览器进行测试,面临效率和稳定性的影响,因此出现了 Headless Browser, 3年前,无头浏览器 PhantomJS 已经如火如荼出现了,紧跟着 NightmareJS 也成为一名巨星。无头浏览器带来巨大便利性:页面爬虫、自动化测试、WebAutomation...
用过PhantomJS的都知道,它的环境是运行在一个封闭的沙盒里面,在环境内外彻底不可通讯,包括API、变量、全局方法调用等。web
So, Chrome59 推出了 headless mode,Chrome59版支持的特性,所有能够利用:
ES2017
ServiceWork(PWA测试随便耍)
无沙盒环境
无痛通信&API调用
无与伦比的速度
...chrome
如今有两个方式获取支持 headless 的 chrome
1.chrome 59 beta 版本:下载连接https://dl.google.com/chrome/...
2.安装安装黄金版 chrome,Chrome Canary :获取方式:npm
brew install Caskroom/versions/google-chrome-canary
3.简单的使用,我就以 chrome 59,演示一下:ubuntu
用一个命令启动 Chrome 做为一个 Headless 服务: ./Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --headless --remote-debugging-port=9222 --disable-gpu http://demo.testfire.net 或者切到google chrome 这个程序目录下 cd /Applications/Google Chrome.app/Contents/MacOS 而后执行 ./Google\ Chrome --headless --remote-debugging-port=9222 --disable-gpu http://demo.testfire.net
使用 Headless Chrome 抓取数据:浏览器
将要使用 Node.js 去链接咱们运行中的 Chrome 实例。你须要确保你已经安装了 Node,才能够继续这个步骤。
让咱们生成一个普通的 Node 项目,只有一个依赖那就是 Chrome Remote Interface 包,它能够帮助咱们与 Chrome 沟通。而后咱们建立一个空白的 index.js 文件。
mkdir my-headless-chrome && cd my-headless-chrome npm init --yes npm install --save chrome-remote-interface touch index.js
如今咱们将要放一些代码到index.js。这个模板例子是由 Chrome 团队提供的。它指示这个浏览器导航到github.com,而后经过 client 里的 Network 属性捕获这个页面上全部的网络请求。
vi index.js 将代码复制到里面: const CDP = require("chrome-remote-interface"); CDP(client => { // extract domains const { Network, Page } = client; // setup handlers Network.requestWillBeSent(params => { console.log(params.request.url); }); Page.loadEventFired(() => { client.close(); }); // enable events then start! Promise.all([Network.enable(), Page.enable()]) .then(() => { return Page.navigate({ url: "https://github.com" }); }) .catch(err => { console.error(err); client.close(); }); }).on("error", err => { // cannot connect to the remote endpoint console.error(err); });
最后启动咱们的 Node 应用。node index.js
咱们能够看到 Chrome 发出的全部的网络请求,然而并无一个实际的浏览器窗口。
$ node index.js http://demo.testfire.net/ http://demo.testfire.net/style.css http://demo.testfire.net/images/logo.gif http://demo.testfire.net/images/header_pic.jpg http://demo.testfire.net/images/pf_lock.gif http://demo.testfire.net/images/gradient.jpg http://demo.testfire.net/images/home1.jpg http://demo.testfire.net/images/home2.jpg http://demo.testfire.net/images/home3.jpg
这是一个很是棒的方式去查看加载了那些资源.
参考连接:https://github.com/yesvods/Bl...
ubuntu 系统下能够参考:https://medium.com/@dschnr/us...
鉴于以上的方式是利用 nodejs直接去操做 headless chrome
我提供一种方式,利用 selenium 的 webdrive 的 chromedriver驱动 chrome59进行 headless chrome 操做
利用 webdrive 的webdriver.ChromeOptions()方法,添加 headless 相关参数,从而驱动 headless的 chrome
下面的代码是进行一个 web 登陆的过程:
#coding:utf-8 from selenium import webdriver url = "http://demo.testfire.net" chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') driver = webdriver.Chrome(chrome_options=chrome_options,executable_path='/Users/xxxx/driver/chromedriver') driver.get('http://demo.testfire.net') driver.find_element_by_xpath('//*[@id="_ctl0__ctl0_LoginLink"]').click() driver.find_element_by_xpath('//*[@id="uid"]').clear() driver.find_element_by_xpath('//*[@id="uid"]').send_keys('admin') driver.find_element_by_xpath('//*[@id="passw"]').send_keys('admin') driver.find_element_by_xpath('//*[@id="login"]/table/tbody/tr[3]/td[2]/input').click() print driver.current_url
最后 print 出登陆成功的当前 url: http://demo.testfire.net/bank...
当浏览器不支持headless模式,能够利用python 的Xvfb实现伪 headless mode,Xvfb只是产生了一个虚拟窗口,浏览器只是没有在当前窗口显示.
简单的列举利用脚本
#coding:utf-8 from selenium import webdriver from xvfbwrapper import Xvfb xvfb = Xvfb(width=1280,height=720) xvfb.start() driver = webdriver.Chrome() driver.get('http://demo.testfire.net') cookies = driver.get_cookies() print cookies driver.close() xvfb.stop()