【问题标题】:page.set('content') doesn't work with dynamic content in phantomjspage.set('content') 不适用于 phantomjs 中的动态内容
【发布时间】:2013-04-28 16:07:26
【问题描述】:

我尝试使用 phantomjs 通过node-phantom 网桥截屏我的页面。这是我正在尝试的:

 var phantom = require('node-phantom');

 phantom.create(function (err, ph) {
            return ph.createPage(function (err, page) {
              return page.set('content', '<html><head></head><body><p>Hello</p></body></html>', function (err, status) {
                  return page.render('./content.png', function (err) {
                    ph.exit();
                  });
                });
            });
          });

这很好用,但是如果我尝试设置包含 javascript 的内容,那就行不通了。请帮助我,为什么它不起作用?

编辑:这不起作用:

var phantom = require('node-phantom');

phantom.create(function (err, ph) {
   return ph.createPage(function (err, page) {
      page.open("about:blank", function(err,status) {
         page.evaluate(function() {        
            document.write('<html><head></head><body><script src="http://code.jquery.com/jquery-1.9.1.min.js"></script><script>$(function(){document.write("Hello from jQuery")})</script></body>');
         });

         setTimeout(function () {
            return page.render('./content.png', function (err) {
                ph.exit();
             }); 
         }, 5000);   
    });         
  });

【问题讨论】:

    标签: node.js phantomjs


    【解决方案1】:

    JavaScript 代码需要一些时间来执行。尝试在设置页面内容和调用render之间有延迟。

    【讨论】:

      【解决方案2】:

      我不确定为什么设置内容不起作用,这似乎是 phantomjs api 的限制。您可以只使用 document.write。

      var phantom = require('node-phantom');
      
      phantom.create(function (err, ph) {
        return ph.createPage(function (err, page) {
          page.open("about:blank", function(err,status) {
            page.evaluate(function() {        
              document.write('<html><body><script>document.write("<h1>Hello From JS</h1>");</script><p>Hello from html</p></body></html>');
            });
            return page.render('./content.png', function (err) {
              ph.exit();
            });
          });
        });         
      });
      

      【讨论】:

      • 不幸的是它不起作用。 page.evaluate 不会被执行。
      【解决方案3】:

      正如阿瑞亚所说,需要时间。这个库可能有一个“onLoadFinished”事件(我使用的节点库有)。您可以通过在此 github 问题底部查看我的示例来处理此问题,而无需任意等待时间:https://github.com/amir20/phantomjs-node/issues/68

      Document.prototype.captureScreenshot = function(next) {
      console.log(">> Rendering screencap for " + this.id)
      var self = this;
      phantom.create(function(ph) {
          ph.createPage(function(page) {
              page.setContent(self.html);
              page.set("viewportSize", {
                  width: 1920,
                  height: 1080
              });
              page.set('onLoadFinished', function(success) {
                  var outputFile = './screenshots/screenshot-' + self.id + '.png';
                  page.render(outputFile);
                  ph.exit();
                  console.log(">> Render complete for " + self.id)
                  if (next)
                      next(outputFile);
              })
          });
      });
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-12
        • 1970-01-01
        • 2012-03-08
        • 1970-01-01
        • 2017-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多