5

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.

Drees
  • 688
  • 1
  • 6
  • 21
Cristi
  • 180
  • 15
  • 2
    If you check devtools->Listeners, you will understand that the button listen for event 'mousedown', and there are also some checks for e.clientX e.clientY. You need a bit of debugging the listener handler in order to achieve what you want to. You could create and dispatch event by checking this: https://stackoverflow.com/questions/24025165/simulating-a-mousedown-click-mouseup-sequence-in-tampermonkey By the way again there are checks/logic which you need to understand. – idmitrov Aug 02 '19 at 23:06

2 Answers2

3

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);
}
Cristi
  • 180
  • 15
2

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.

Nathan Fries
  • 1,464
  • 5
  • 15