Cristi
Cristi

Reputation: 310

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.

Upvotes: 5

Views: 1700

Answers (2)

Cristi
Cristi

Reputation: 310

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);
}

Upvotes: 4

Nathan Fries
Nathan Fries

Reputation: 1514

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.

Upvotes: 2

Related Questions