22

Is it possible to pass variables in a page.evaluate in my case below?

function myFunction(webpage, arg1, arg2){

var page = require('webpage').create();

page.viewportSize = { width: 1920, height: 1080 };

page.open(webpage, function (status){

    if (status == 'success') {

            page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js", function(){

                page.evaluate(function(){

                    arg = arg1 + arg2;
                    console.log(arg);

                });

            });

    } 

    else { phantom.exit(); }

});

}

I tried several methods found on the internet but nothing actually impossible to get to its variables.

Thank you in advance for your help :)

Antoine
  • 485
  • 3
  • 5
  • 12

3 Answers3

70

As usual, the answer is clearly stated in the documentation of evaluate function:

As of PhantomJS 1.6, JSON-serializable arguments can be passed to the function. In the following example, the text value of a DOM element is extracted. The following example achieves the same end goal as the previous example but the element is chosen based on a selector which is passed to the evaluate call:

The example that follows demonstrates the usage:

var title = page.evaluate(function(s) {
    return document.querySelector(s).innerText;
}, 'title');
console.log(title);
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Ariya Hidayat
  • 12,523
  • 3
  • 46
  • 39
  • 3
    This is not what I want to do, I want to be able to get to to variables passed as parameters to my function. – Antoine Mar 12 '13 at 08:59
  • 7
    Did you even analyze and try my answer? I believe what you want to do is: page.evaluate(function(arg1, arg2){ console.log(arg1+arg2); }, arg1, arg2);. – Ariya Hidayat Mar 12 '13 at 09:33
  • 1
    Yes I want do that ! Thanks you very much ! :D – Antoine Mar 12 '13 at 09:57
  • 11
    I was a bit thrown off too, before playing around with it. I believe this is more clear: `page.evaluate(function(str, obj) { ... }, 'myString', {foo: 'bar'});` – David Calhoun Dec 30 '14 at 02:09
  • 4
    I know this answer is from 2013, but it really should be edited so that the syntax that is explained in the comment is the syntax shown in the answer itself, so that visitors in 2017 (or later =) don't have to read through a comment thread to get the *actual* answer. – Mike 'Pomax' Kamermans Aug 16 '17 at 21:57
2

I have phantomjs 1.5.0, so instead of compiling 1.6 or higher version I went for an alternative solution:

So I've saved arguments to selectors.js file

-------------selectors.js starts----------------
var selectors = "div.nice"
-------------selectors.js   ends----------------

and then injected them into the page:

page.injectJs("selectors.js");

More details can be found here: http://phantomjs.org/api/webpage/method/inject-js.html

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Kostyantyn
  • 5,041
  • 3
  • 34
  • 30
0

I use phantom 4.0.4, below works for me, https://www.npmjs.com/package/phantom

var arg = 'test'
page.evaluate(function(arg) {
    console.log(arg)
}, arg);