Phantomjs的高度封裝:nightmare

jopen 10年前發布 | 30K 次閱讀 nightmare JavaScript開發

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.baiduhome.net/lib/view/home/1412738918764

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!