page.set('content') doesn't work with dynamic content in phantomjs

匿名 (未验证) 提交于 2019-12-03 02:33:02

问题:

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

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?

EDIT: This doesn't 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);        });            }); 

回答1:

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



回答2:

I am not sure why set content does not work, It seems to be a limitation of the phantomjs api. You can just use 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();       });     });   });          }); 


回答3:

as ariya mentioned, time is needed. there is likely an 'onLoadFinished' event for this library (there is for the node lib i use). you can handle this without an arbitrary wait time by seeing my example at the bottom of this github issue: 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);         })     }); }); 

}



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!