page.set('content')不适用于phantomjs中的动态内容 [英] page.set('content') doesn't work with dynamic content in phantomjs

查看:200
本文介绍了page.set('content')不适用于phantomjs中的动态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用phantomjs通过 node-phantom 网桥来捕获页面.这是我正在尝试的:

I tried to use phantomjs for screen capturing my page with node-phantom bridge. Here is what I'm trying:

 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的内容,那是行不通的.请帮助我,为什么它不起作用?

That works fine, but if I try to set content which contains javascript, that doesn't work. Please help me, why does it not work?

编辑:这不起作用:

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);   
    });         
  });

推荐答案

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

JavaScript code needs some time to execute. Try to have a delay between setting the page content and calling render.

这篇关于page.set('content')不适用于phantomjs中的动态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆