简体   繁体   中英

Click function is not working on a button

I try to create an application to log in to https://www.easports.com/fifa/ultimate-team/web-app/# using JS, and I am not able to click on the Login button.

Calling click on the button element.

document.getElementsByTagName("button")[0].click()

Nothing happens. If I move with the mouse and click it, it works.

It looks like they are using mousedown and mouseup event listeners to determine if both events occurred while the cursor is over the button. This is an example of of a button that doesn't want to be clicked automatically using a script.

Thanks a lot idmitrov. I've managed to make it work using this:

var targetNode = document.getElementsByTagName("button")[0];
if (targetNode) {
    //--- Simulate a natural mouse-click sequence.
    triggerMouseEvent (targetNode, "mouseover");
    triggerMouseEvent (targetNode, "mousedown");
    triggerMouseEvent (targetNode, "mouseup");
    triggerMouseEvent (targetNode, "click");
}
else
    console.log ("*** Target node not found!");

function triggerMouseEvent (node, eventType) {
    var clickEvent = document.createEvent ('MouseEvents');
    clickEvent.initEvent (eventType, true, true);
    node.dispatchEvent (clickEvent);
}

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