2015-06-19 110 views
1

當我按照本教程(http://phantomjs.org/screen-capture.html)進行屏幕截圖時,我遇到了有關動態數據可視化的一些問題。如何使用phantomjs拍攝動態數據可視化屏幕截圖

該教程,它採用像一些代碼:

var page = require('webpage').create(); 
page.open('http://localhost:8080/index.html', function() { 
    page.render('screenshot.png'); 
    phantom.exit(); 
}); 

但這似乎只與靜態頁面的工作。我不知道我是否有一些用戶交互,並使該頁面改變(如鼠標點擊更改顏色等),我怎麼能顯示當前屏幕

如果 phantomjs可以這樣不工作,任何人都可以提出一些其他的解決方案

回答

1

當然,只需在page.open()之後和page.render()之前添加您想要的功能即可。

page.open('http://localhost:8080/index.html', function() { 

    /** 
    * Add your events and actions here... 
    * or call JS functions of the page itself... 
    */ 

    page.evaluate(function() { 

    /** 
    * Inject a <style text/css> tag in the head, 
    * which set the bgcolor 
    */ 

     var style = document.createElement('style'), 
     text = document.createTextNode('body { background: red }'); 
     style.setAttribute('type', 'text/css'); 
     style.appendChild(text); 
     document.head.insertBefore(style, document.head.firstChild); 

    }); 

    page.render('screenshot.png'); 
    phantom.exit(); 
}); 

請記住,你的JavaScript在這裏工作,你可以將任意輔助腳本,像CasperJS或jQuery和加載的頁面上使用其功能。

爲了尋找靈感:

+0

謝謝,但我沒有抓住你,目前用戶交互代碼在index.html中,大多數是DOM操作,我想知道如何將它們移動到這裏?你能告訴我一個例子,如點擊一個div並將其背景改爲紅色並拍攝該屏幕截圖? – Kuan

+0

您不必移動它們。只需從PhantomJS屏幕截圖腳本調用index.html的Javascript函數即可。 –

+0

我已經添加了背景顏色更改爲我的答案。點擊一個元素(div),請閱讀其他的stackoverflow答案,因爲它已經在那裏描述過了。 –

相關問題