0

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.

levshkatov
  • 477
  • 2
  • 5
  • 16
  • @ArtjomB. It helped. I changed code and have another problem in `waitFor` function – levshkatov Mar 20 '16 at 15:59
  • @ArtjomB. Ok, and how to deal with it? I need to click on button until it disappears. What is another solution except loop with timeout? – levshkatov Mar 20 '16 at 17:52
  • Which PhantomJS version do you use? Please register to the `onConsoleMessage`, `onError`, `onResourceError`, `onResourceTimeout` events ([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf#file-1_phantomerrors-js)). Maybe there are errors. – Artjom B. Mar 20 '16 at 18:24
  • Looks fine to me. Have you tried to use different ways to click the button? [PhantomJS; click an element](http://stackoverflow.com/q/15739263/1816580) – Artjom B. Mar 20 '16 at 18:25
  • @ArtjomB. No errors, thanks for handling code. I tried a function `click(el)`. If I put it outside `evaluate` it's an error of course. But if I put it inside, it's also an error: `undefined is not a constructor`. I use the latest 2.1 version – levshkatov Mar 20 '16 at 18:31
  • Are you sure the default 3 second timeout (3rd argument) of `waitFor` is enough? – Artjom B. Mar 20 '16 at 18:36
  • @ArtjomB. I can increase it to 10sec for example, but it won't help. Definitely problem is with clicking, so how to use a function `click(el)` from that topic to solve the problem? – levshkatov Mar 20 '16 at 18:38
  • In the same way that you do here: `click($("button.example")[0])`, but remember that `click` must also be defined in the page context. There are also different ways to click there. Also, you might try to use PhantomJS 1.9.8, because PhantomJS 2.x is known to hide some errors. – Artjom B. Mar 20 '16 at 18:51
  • @ArtjomB. I tried 1.9.8 but it also didn't help. Console ends with `waitFor() timeout`. What do you mean by different ways to click on button? – levshkatov Mar 20 '16 at 19:01

0 Answers0