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();
});
}
For some reason, I just can't get this to work. PhantomJS is complaining: "Error: cannot access member 'evaluate' of deleted QObject". Is it because I am having multiple page.evaluates?
PhantomJS is asynchronous. In this case waitFor()
is asynchronous, so you need to close the page
after you've done with it. You need to move
page.close();
callback.apply();
into the last function that will be executed which is the callback of waitFor()
. You might want to change waitFor
a little bit, so that there is another callback when the timeout is reached which is the error branch which also requires the page closing and callback:
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);
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.