我尝试创建一个使用JS登录https://www.easports.com/fifa/ultimate-team/web-app/#的应用程序,但无法单击“登录”按钮。
调用单击按钮元素。
document.getElementsByTagName("button")[0].click()
没发生什么事。如果我用鼠标移动并单击它,它将起作用。
最佳答案
非常感谢idmitrov。我设法使它使用此:
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);
}