I\'m new to Javascript and PhantomJS, but my seemly simple objective has proven to be harder to achieve than expected. I want to write a script that would load a website, an
Aaron's answer didn't work for me. Maybe it was valid back in 2013, but now (2016), it doesn't work anymore with the current version. In Aaron's example, foo
returns a promise, not a value. console.log(foo)
outputs Promise { <pending> }
.
Here's what I figured out :
page
.evaluate( function(){ return window.foo })
.then ( function(foo){ console.log "foo = ", foo});
Now you really get the value of foo.
What you need to understand is that phantomJS has two JavaScript environments and those two are independent of each other. The inner one is the document script (which you have in any browser). The outer one is controlling what phantomJS should do. It simulates the user.
So in a sense you need to tell phantomJS "the user opened the JavaScript console any typed ...
". The evaluate command does this.
So to read the value of the variable foo
, you write this code:
var foo = page.evaluate(function() {
return document.foo;
});
Note: The document
isn't strictly necessary but it helps to keep the two environments apart in the head of the developer.