PhantomJS 无法访问已删除 QObject 的成员 'evaluate'

PhantomJS cannot access member 'evaluate' of deleted QObject

function checkMessages(user, password, callback) {  
    var page = require('webpage').create();  
    page.open('http://mywebpage.com', function (status) {  
        if (status === 'fail') {  
            console.log(user + ': ?');  
        } else {  
            page.evaluate(function (user, password) {  
                document.querySelector('input[name=username]').value = user;  
                document.querySelector('input[name=password]').value = password;  
                document.querySelector('button[name=yt0]').click();  
            }, user, password);  
            waitFor(function() {  
                return page.evaluate(function() {  
                    var el = document.getElementById('fancybox-wrap');  
                    if (typeof(el) != 'undefined' && el != null) {  
                        return true;  
                    }  
                    return false;  
                });  
            }, function() {  
                var messageCount = page.evaluate(function() {  
                    var el = document.querySelector('span[class=unread-number]');  
                    if (typeof(el) != 'undefined' && el != null) {  
                        return el.innerText;  
                    }  
                    return 0;  
                });  
                console.log(messageCount);  
            });  
        }  
        page.close();  
        callback.apply();  
    });
}

出于某种原因,我无法让它工作。 PhantomJS 正在抱怨:"Error: cannot access member 'evaluate' of deleted QObject"。是不是因为我有多个 page.evaluates?

PhantomJS 是异步的。在这种情况下 waitFor() 是异步的,因此您需要在完成后关闭 page。你需要移动

page.close();
callback.apply();

进入将要执行的最后一个函数,即 waitFor() 的回调。您可能需要稍微更改 waitFor ,以便在达到超时时有另一个回调,这是错误分支,也需要页面关闭和回调:

function waitFor(testFx, onReady, timeOutMillis) {
    var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000,
        start = new Date().getTime(),
        condition = false,
        interval = setInterval(function() {
            if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
                condition = testFx();
            } else {
                var error = null;
                if(!condition) {
                    error = "'waitFor()' timeout";
                    console.log(error);
                } else {
                    console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
                    clearInterval(interval);
                }
                onReady(error);
            }
        }, 250);
};