Home > Mobile >  How to use JavaScript to click a div like button on an HTML page?
How to use JavaScript to click a div like button on an HTML page?

Time:11-10

I'm currently trying to automate a game called Lyrics Training (DIV Buttons

Any help would be appreciated! Thanks!

CodePudding user response:

Give the button div element an attribute of role="button". This should work fine.

CodePudding user response:

If I am not misinterpreting anything, then it sounds like you are trying to automate a game on some website. I'm not sure exactly how they coded the game, but there is a chance the code doesn't actually listen for "click" events.

It seems like your code only works if the website's code responds to click events such as:

div.addEventListener("click", somefunction);
//or
btn.addEventListener("click", somefunction);

In this case, your code would still work on divs.

However, they could be responding to mousedown events:

div.addEventListener("mousedown", somefunction);

If this is the case, you might want to read up on invoking specific events: https://developer.mozilla.org/en-US/docs/Web/Events/Creating_and_triggering_events

But please keep in mind that many websites have code preventing this stuff.

  • Related