简体   繁体   中英

Click a button using javascript via console in browser

I'm trying to click a specific button on a page using javascript by typing the javascript code into chrome console. It doesn't work. Is it possible that the website can disable clicking an element via Javascript?

I've tried to call all button with a specific class to a variable, and then used the.click() method on a specific point.

var zz = document.getElementsByClassName("red_buttons");
zz[0].click();

Ideally this would cause the page to act in the same way as a regular mouse click (which actually works), but it does absolutely nothing as far as I know.

You can use this code to click on a specific button on the page. In the section, you should type the ID value of the button you want to click. You can desing the clickfunc() function in any flexibility you want. It will be enough for you to just type the button.click(); statement into the function and it will click with the simplest version.

var button = document.getElementById('<ElementID>');

function sleep(ms) {
      return new Promise(resolve => setTimeout(resolve, ms));
   }

async function clickfunc(){
    while(true){
        button.click();
        console.log("Tried. Next one in 10 sec.");
        await sleep(10000);
       }
  }

clickfunc();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM