0

I started using PhantomJS a few days ago with NodeJS. I am using this library to integrate with it: https://github.com/amir20/phantomjs-node. Everything worked perfectlly but when I tried to continue the flow of my applcation after a page load (from the callback) something went wrong.

function doStuff () { page.open("http://stackoverflow.com/") .then(function (status) { function responseHandler(status) { console.log("loaded"); iAmHere(); console.log("Here Again"); } function loginAction() { var btn = document.querySelector("#button"); btn.click(); } page.property('onLoadFinished', responseHandler); page.evaluate(loginAction) } ); } function iAmHere (){ console.log("iAmHere"); } 

The #button element triggers some page load, The responseHandler function is called and the output is:

info: loaded

and the function iAmHere doesn't get called at all and niether the log that follows the call. What did I do wrong?

Thanks!

1 Answer 1

3

The reason that iAmHere() isn't being invoked when the onLoadFinished event fires is because the function you provided, responseHandler, is actually being executed by the PhantomJS JavaScript engine, and not Node.js.

Therefore, it doesn't have access to the iAmHere() function, which you defined within your Node.js script.

You could, instead, be notified when the page finishes loading like this:

var phantom = require('phantom'); var sitepage = null; var phInstance = null; phantom.create() .then(instance => { phInstance = instance; return instance.createPage(); }) .then(page => { sitepage = page; return page.open('https://stackoverflow.com/'); }) .then(status => { console.log(status); // Page loaded, do something with it return sitepage.property('content'); }) .then(content => { console.log(content); sitepage.close(); phInstance.exit(); }) .catch(error => { console.log(error); phInstance.exit(); }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.