简体   繁体   English

点击功能不适用于按钮

[英]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. 我尝试创建一个使用JS登录到https://www.easports.com/fifa/ultimate-team/web-app/#的应用程序,但无法单击“登录”按钮。

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. 看起来他们正在使用mousedownmouseup事件侦听器来确定在光标位于按钮上方时是否同时发生了两个事件。 This is an example of of a button that doesn't want to be clicked automatically using a script. 这是不想使用脚本自动单击的按钮的示例。

Thanks a lot idmitrov. 非常感谢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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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