How to execute page.render inside page.evaluate?

I want to display my page and exit PhantomJS in the evaluation function, because I want to call it when a certain event is triggered.

I tried something like this:

page.evaluate(page, function (page, phantom) { //do some stuff on my page //i want to execute this in an eventhandler of my page though thats not the problem page.render('imgName.png'); page.render('pdfName.pdf'); phantom.exit(); }, page, phantom); 

This does not work in my example because page.render seems undefined. Maybe there is a serializer for arguments that does not serialize obj functions?

Is it possible? Does anyone know a solution to my problem?

I know that I can set the while loop in my evaluation function and prevent it from completing, and after that this page will be displayed outside. I do not like this.

+7
javascript phantomjs
source share
1 answer

page.evaluate() - a sandbox. The function you pass is evaluated in the context of the page, and all arguments must be serialized to succeed. You cannot pass page or phantom to the page context. docs say the following:

Note. The arguments and return value of the evaluate function should be a simple primitive object. A rule of thumb: if it can be serialized via JSON, then this is normal.

Closures, functions, DOM nodes, etc. will not work!

This is what page.onCallback for:

 page.onCallback = function(data){ if (data.exit) { page.render('imgName.png'); page.render('pdfName.pdf'); phantom.exit(); } }; page.evaluate(page, function () { //do some stuff on my page //i want to execute this in an eventhandler of my page though thats not the problem window.callPhantom({ exit: true }); }); 
+16
source share

All Articles