How to trigger click on a button

后端 未结 2 1480
醉梦人生
醉梦人生 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:40

    What about Vanilla JS, noone prohibited?

    Such as "onlick/click"?

    function ae(a,b,c) {
     if (a.addEventListener)
         a.addEventListener (b,c,false);
     else if (a.attachEvent)
         a.attachEvent ('on'+b,c); 
    }
    function re(a,b,c) {
     if (a.removeEventListener) 
        a.removeEventListener (b,c,false);
     if (a.detachEvent)
        a.detachEvent ('on'+b,c); 
    }
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题