【问题标题】:How to use phantomjs to take screen shot with dynamic data visualization如何使用 phantomjs 进行动态数据可视化截屏
【发布时间】:2015-06-19 16:25:10
【问题描述】:

当我按照本教程 (http://phantomjs.org/screen-capture.html) 进行屏幕截图时,我遇到了一些关于动态数据可视化的问题。

在该教程中,它使用了如下代码:

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

但这似乎只适用于静态页面。我想知道我是否进行了一些用户交互并更改了该页面(例如鼠标单击更改颜色等),如何我可以捕获显示当前屏幕

如果 phantomjs 不能以这种方式工作,谁能提出其他解决方案

【问题讨论】:

    标签: javascript phantomjs screenshot


    【解决方案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,并在加载的页面上使用它们的功能。

    灵感:

    【讨论】:

    • 谢谢,但我没听懂,目前用户交互代码在index.html中,大部分是DOM操作,我想知道如何将它们移到这里?你能给我看一个例子,比如点击一个 div 并将其背景更改为红色并截取屏幕截图吗?
    • 您不必移动它们。只需从 PhantomJS 截图脚本中调用 index.html 的 Javascript 函数即可。
    • 我在我的答案中添加了背景颜色变化。要单击一个元素 (div),请阅读其他 stackoverflow 答案,因为它已经在那里进行了描述。
    猜你喜欢
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    相关资源
    最近更新 更多