PhantomJS无法访问已删除QObject的成员“评估”

PhantomJS cannot access member 'evaluate' of deleted QObject

本文关键字:成员 评估 QObject 删除 访问 PhantomJS      更新时间:2023-09-26
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抱怨:"错误:无法访问已删除QObject的成员'评估'"。是因为我有多个页面评估吗?

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