繁体   English   中英

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

[英]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抱怨:“错误:无法访问已删除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);
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM