Search code examples
javascriptjquerytampermonkey

auto click a button that doesn't have ID in tampermonkey


I would be gratfull if someone could help make a script that runs in Tampermonkey that will allow me to auto click a button that doesn't have an ID after a certain delay.

here is the JS path of the button

document.querySelector("#al_login > div.col-sm-8.container.paddingInBoxExtra.roundCornerExtra > div:nth-child(2) > input.row.btn.primary-btn.pull-right.marginBottomNone")

Solution

  • Use setTimeout for delay and click() for trigger your button.

    Example:

    // ==UserScript==
    // @name         Give Name Your Userscript
    // @namespace    http://tampermonkey.net/
    // @version      0.1
    // @description  Your Script Description
    // @author       You
    // @match        http://*/* [URL where you want to run script]
    // @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
    // @grant        none
    // ==/UserScript==
    
    (function() {
      'use strict';
    
      var element = document.querySelector("#al_login > div.col-sm-8.container.paddingInBoxExtra.roundCornerExtra > div:nth-child(2) > input.row.btn.primary-btn.pull-right.marginBottomNone");
      if (element) {
        window.setTimeout(function() {
          element.click()
        }, 2000);
    
      }
      
    })();