I need to simulate click on a button until it disappears. I use PhantomJS for doing it.
numbSongs
should be different if this code works properly. If I use return false
like in example, it doesn't work (the necessary condition never matches). And if I change it to true
it works but numbSongs
is the same.
It's code of waitFor function from official example of PhantomJS:
"use strict";
function waitFor(testFx, onReady, timeOutMillis) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s
start = new Date().getTime(),
condition = false,
interval = setInterval(function() {
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
// If not time-out yet and condition not yet fulfilled
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
} else {
if(!condition) {
// If condition still not fulfilled (timeout but condition is 'false')
console.log("'waitFor()' timeout");
phantom.exit(1);
} else {
// Condition fulfilled (timeout and/or condition is 'true')
console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
clearInterval(interval); //< Stop this interval
}
}
}, 250); //< repeat check every 250ms
};
I'm not sure if it's waitFor condition problem or it's problem with simulating a click on button.
UPD:
I try this code for checking if there is button on a page or not. I expect from this code doing: clicking on button, waiting some time, clicking again if button is alive and continue it until button disappears. Then there should be a log Button disappears
. Where my code is wrong?
waitFor(function(){
return page.evaluate(function(){
var buttonMore = $("button.example");
buttonMore.click();
if (buttonMore.length > 0){
return false;
} else {
return true;
}
});
}, function(){
console.log("Button disappears");
phantom.exit();
});
SOLVED
The problem was that I needed to put clicking(buttonMore[0]);
inside if
.