2016-08-12 106 views
0

我一直在用這一個撞我的頭。CasperJS屏幕截圖給出了一個空白屏幕

我在casperjs使用phantomjs 1.9.8

我試圖捕捉URL的圖像,並將其保存一個文件運行1.1.0-β3。

這是我的命令:

casperjs --proxy-type=none --ssl-protocol=any /home/casper-capture.js http://url.com/demo/demo /home/demoScreenshot.png 

這是我卡斯珀 - capture.js

/** 
* capture image from url and save it to file. 
**/ 
var casper = require('casper').create({ 
    verbose: true, 
    logLevel: "debug", 
    viewportSize: { 
     width: 2300, 
     height: 1200 
    }, 
    pageSettings: { 
     webSecurityEnabled: false, 
     loadImages: true,  // The WebPage instance used by Casper will 
     loadPlugins: true   // use these settings 
    } 
}); 
// delay before image capturing 
var delay = 60000; 
//timeout delay for loading 
var timeoutForLoading = 10 * 60000; 
// image source url. 
var url = casper.cli.args[0]; 
// image output path 
var path = casper.cli.args[1]; 

casper.start().zoom(4).thenOpen(url, function urlCaptureClouser() { 
    this.wait(delay, function(){ 
     casper.waitFor(function check() { 
      return this.evaluate(function() { 
       return document.querySelectorAll('.fa-spin').length + 
         document.querySelectorAll('.chart-loading').length == 0; 
      }); 
     }, function then() { 
      this.capture(path); 
     }, function then() { 
      this.capture(path); 
     }, timeoutForLoading); 
    }); 
}); 

casper.run(); 

我得到一個 「空白」 的屏幕。 :-(

當我在我的本地系統上運行它。它的工作原理!我不知道我做錯了。

可有人請針點我的問題?

控制檯日誌(以錯誤日誌):

[info] [phantom] Starting... 
[info] [phantom] Running suite: 2 steps 
[debug] [phantom] opening url: <URL>, HTTP GET 
[debug] [phantom] Navigation requested: url=<URL>, type=Other, willNavigate=true, isMainFrame=true 
[debug] [phantom] url changed to "<URL>" 
Error: TypeError: 'undefined' is not an object (evaluating 'Object.assign.apply') 
[debug] [phantom] Successfully injected Casper client-side utilities 
[info] [phantom] Step urlCaptureClouser 2/2 <URL> (HTTP 200) 
[info] [phantom] Step urlCaptureClouser 2/2: done in 329ms. 
[info] [phantom] Step _step 3/3 <URL> (HTTP 200) 
[info] [phantom] Step _step 3/3: done in 351ms. 
[info] [phantom] wait() finished waiting for 60000ms. 
[info] [phantom] Step _step 4/4 <URL> (HTTP 200) 
[info] [phantom] Step _step 4/4: done in 60358ms. 
[info] [phantom] waitFor() finished in 41ms. 
[info] [phantom] Step then 5/5 <URL> (HTTP 200) 
[debug] [phantom] Capturing page to /home/dmeo1991.png 
[info] [phantom] Capture saved to /home/dmeo1991.png 
[info] [phantom] Step then 5/5: done in 60674ms. 
[info] [phantom] Done 5 steps in 60674ms 
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file:///usr/lib/node_modules/casperjs/bin/bootstrap.js. Domains, protocols and ports must match. 
+0

你是如何安裝PhantomJS和CasperJS的?是通過NPM嗎?請註冊到'resource.error','page.error','remote.message'和'casper.page.onResourceTimeout'事件([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf#file -2_caspererrors-JS))。也許有錯誤。 –

+0

謝謝@ArtjomB。讓我試試看。 – TechnoCorner

+0

請查看完整的調試日誌:https://justpaste.it/x8co @ArtjomB。 – TechnoCorner

回答

2

你的日誌顯示「錯誤:類型錯誤:‘未定義’不是(評估‘Object.assign.apply’)的對象」,這可能是因爲沒有一個負責黑屏, JavaScript實際上被執行了。

您或者需要upgrade to PhantomJS 2.x(PhantomJS 1.x背後的引擎現在已超過5年)或添加polyfill。我已經從MDN複製了polyfill:

casper.on('page.initialized', function(){ 
    this.evaluate(function(){ 
     // Polyfill... 
     if (typeof Object.assign != 'function') { 
      Object.assign = function(target) { 
      'use strict'; 
      if (target == null) { 
       throw new TypeError('Cannot convert undefined or null to object'); 
      } 

      target = Object(target); 
      for (var index = 1; index < arguments.length; index++) { 
       var source = arguments[index]; 
       if (source != null) { 
       for (var key in source) { 
        if (Object.prototype.hasOwnProperty.call(source, key)) { 
        target[key] = source[key]; 
        } 
       } 
       } 
      } 
      return target; 
      }; 
     } 
    }); 
}); 

casper.start(...)... 
+0

非常感謝你!這工作像魅力! – TechnoCorner