Javascript 从PhantomJS onLoadFinished回调调用函数

Javascript 从PhantomJS onLoadFinished回调调用函数,javascript,node.js,phantomjs,browser-automation,Javascript,Node.js,Phantomjs,Browser Automation,几天前,我开始在NodeJS中使用PhantomJS。 我正在使用此库与它集成:。 一切都很完美,但当我尝试在页面加载(来自回调)后继续应用程序流时,出现了一些问题 function doStuff () { page.open("http://stackoverflow.com/") .then(function (status) { function responseHandler(status) {

几天前,我开始在NodeJS中使用PhantomJS。 我正在使用此库与它集成:。 一切都很完美,但当我尝试在页面加载(来自回调)后继续应用程序流时,出现了一些问题

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");
}
#button元素触发一些页面加载,调用responseHandler函数,输出为:

信息:已加载

函数iAmHere根本没有被调用,调用后的日志也没有被调用。 我做错了什么


谢谢

当触发
onLoadFinished
事件时,
iAmHere()
没有被调用的原因是,您提供的函数
responseHandler
,实际上是由PhantomJS JavaScript引擎执行的,而不是Node.js

因此,它无法访问Node.js脚本中定义的
iAmHere()
函数

相反,当页面完成加载时,您可能会收到如下通知:

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();
    });

当触发
onLoadFinished
事件时,没有调用
iAmHere()
,原因是您提供的函数
responseHandler
,实际上是由PhantomJS JavaScript引擎执行的,而不是Node.js

因此,它无法访问Node.js脚本中定义的
iAmHere()
函数

相反,当页面完成加载时,您可能会收到如下通知:

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();
    });