How do I get Greasemonkey to click on a button that only appears after a delay?

That code has errors. Use Firefox’s error console (CtrlShiftJ) to see them. Using jslint to check your code, can be helpful too.

Anyway, this is a common Greasemonkey problem. Use the waitForKeyElements() utility to handle the delayed appearance of that button. Use jQuery to simplify the code (and make it more robust and portable).

So your script would become:

// ==UserScript==
// @name     _YOUR_NAME
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

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

//-- Value match is case-sensitive
waitForKeyElements (
    //"#btn_submit input[type="submit"][value*='Click Me Now']",
    "input[type="submit"][value*='Click Me Now']",
    clickSubmitBtnWhenItAppears
);

Leave a Comment