我正在一个自动化项目中,尝试在网站上单击一个按钮。我设法识别出该元素,但是当我尝试使用document.createEvent时,它告诉我该对象不支持它。有任何想法吗?
function categoryClick() {
var events = ["mousemove", "mouseover", "focus", "mousedown", "mouseup", "click"];
var combobox = document.getElementById("category");
var a = combobox.childNodes;
var target = a[0].childNodes[0];
for (var i = 0; i < events.length; i++) {
var eventObject = document.createEvent("Event");
eventObject.initEvent(events[i], true, false);
target.dispatchEvent(eventObject);
}
}
最佳答案
createEvent
已折旧
参见:https://developer.mozilla.org/en-US/docs/Web/API/Document/createEvent
它已被替换为Event()
参见:https://developer.mozilla.org/en-US/docs/Web/API/Event/Event
因此,对于您的代码,这应该可以作为重构:
function categoryClick() {
var events = ["mousemove", "mouseover", "focus", "mousedown", "mouseup", "click"];
var combobox = document.getElementById("category");
var a = combobox.childNodes;
var target = a[0].childNodes[0];
for (var i = 0; i < events.length; i++) {
var eventObject = new Event(events[i], {"bubbles":true, "cancelable":false});
target.dispatchEvent(eventObject);
}
}