JavaScript-对象

时间:2018-10-09 12:20:20

标签: javascript events automation click

我正在一个自动化项目中,尝试在网站上单击一个按钮。我设法识别出该元素,但是当我尝试使用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);
  }
}

1 个答案:

答案 0 :(得分:0)

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