1

The target page has this source:

jQuery(function () {
    var delay = 1000; //1 sn
    var id = "0b9cfb9f62ef7a2";
    jQuery.ajax({
        "method":"get",
        "url":"?ajax="+id,
        "success": function (json) {
            //alert(json);
            json = JSON.parse(json);
            setTimeout(function(){
                if(json.data != ""){
                    jQuery("#button").attr("class","btn btn-block btn-danger");
                    jQuery("#button").text(json.data);
                }else{
                    jQuery("#button").click(function(){location.href=json.url});
                    jQuery("#button").attr("class","btn btn-block btn-success");
                    jQuery("#button").text("Download!");
                }
            },delay);
        },
        "error":function(){
            alert("XXX");
        }
    });
});

What I'm trying to do is to click the Download! button once it appears after a few seconds.

I tried to by using this code:

waitForKeyElements ("#button:contains('Download')", clickSubmitBtnWhenItAppears);

function clickSubmitBtnWhenItAppears (jNode) {
    var clickEvent = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    jNode[0].dispatchEvent (clickEvent);
}

But it doesn't work! I think the problem is on #button, but I couldn't figure it out. Sorry for my weak knowledge in JS.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Gregor Isack
  • 1,111
  • 12
  • 25

1 Answers1

2

Your code is waiting for the existence of #button. What it needs to do is wait both for the existence of #button and for button to have the text "Download".

In this case, you should be able to do it merely by tuning the jQuery selector like so:

waitForKeyElements ("#button:contains('Download')", clickSubmitBtnWhenItAppears);


For more complex cases, you might need to use more fine tuning with waitForKeyElements().

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295