Phantomjs的高度封装:nightmare

jopen 9年前

nightmare是PhantomJS的高级封装,让你能够实现浏览器自动化任务。PhantomJS 是一个基于WebKit的服务器端 JavaScript API。它全面支持web而不需浏览器支持,其快速,原生支持各种Web标准: DOM 处理, CSS 选择器, JSON, Canvas, 和 SVG。PhantomJS可以用于页面自动化,网络监测,网页截屏,以及无界面测试等。

示例

实现在Yahoo上搜索:

var Nightmare = require('nightmare');  new Nightmare()    .goto('http://yahoo.com')      .type('input[title="Search"]', 'github nightmare')      .click('.searchsubmit')      .run(function (err, nightmare) {        if (err) return console.log(err);        console.log('Done!');      });

Or, let's extract the entirety of Kayak's home page after everything has rendered:

var Nightmare = require('nightmare');  new Nightmare()    .goto('http://kayak.com')    .evaluate(function (page) {      return document.documentElement.innerHTML;    }, function (res) {      console.log(res);    })    .run();

Or, here's how you might automate a nicely abstracted login + task on Swiftly:

var Nightmare = require('nightmare');  var Swiftly = require('nightmare-swiftly');  new Nightmare()    .use(Swiftly.login(email, password))    .use(Swiftly.task(instructions, uploads, path))    .run(function(err, nightmare){      if (err) return fn(err);      fn();    });

项目主页:http://www.open-open.com/lib/view/home/1412738918764