I have this code running in the phantomjs. I don’t know why form.elements keeps returning null to me. I ran the same code on chrome developer console and got the right result i want.
I’m pretty new to javascript and everything related. Please shed some light.
var page = require('webpage').create();
page.open('http://www.kayak.com', function (status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
var form = page.evaluate(function(){
return document.getElementById('searchform');
});
console.log(form.elements[2].value);
}
phantom.exit();
});
You can’t pass a non-primitive object through
evaluate. This is mentioned in the documentation:Although it is the most common mistake when using PhantomJS, this is rather easy to solve. Just make sure you do all the processing as much as you can within
evaluateand then return back a simple object. For details, please study the documentation carefully and learn from all the included examples.