How to trigger click on a button

后端 未结 2 1497
醉梦人生
醉梦人生 2020-11-29 11:49

I\'ve this page. I need to trigger a click on the BUY NOW button on this page using AngularJS.

I\'ve tried these ways to click on this \"BUY NOW\" in content script(

2条回答
  •  隐瞒了意图╮
    2020-11-29 12:43

    Try with this code; it simulates a mouse left click on the element by a quick succession of mousedown, mouseup and click events fired in the center of the button:

    var simulateMouseEvent = function(element, eventName, coordX, coordY) {
      element.dispatchEvent(new MouseEvent(eventName, {
        view: window,
        bubbles: true,
        cancelable: true,
        clientX: coordX,
        clientY: coordY,
        button: 0
      }));
    };
    
    var theButton = document.querySelector('ul form button');
    
    var box = theButton.getBoundingClientRect(),
            coordX = box.left + (box.right - box.left) / 2,
            coordY = box.top + (box.bottom - box.top) / 2;
    
    simulateMouseEvent (theButton, "mousedown", coordX, coordY);
    simulateMouseEvent (theButton, "mouseup", coordX, coordY);
    simulateMouseEvent (theButton, "click", coordX, coordY);
    

提交回复
热议问题