How can I trigger a JavaScript event click

后端 未结 9 2225
迷失自我
迷失自我 2020-11-22 02:56

I have a hyperlink in my page. I am trying to automate a number of clicks on the hyperlink for testing purposes. Is there any way you can simulate 50 clicks on the hyperlink

9条回答
  •  天命终不由人
    2020-11-22 03:35

    .click() does not work with Android (look at mozilla docs, at mobile section). You can trigger the click event with this method:

    function fireClick(node){
        if (document.createEvent) {
            var evt = document.createEvent('MouseEvents');
            evt.initEvent('click', true, false);
            node.dispatchEvent(evt);    
        } else if (document.createEventObject) {
            node.fireEvent('onclick') ; 
        } else if (typeof node.onclick == 'function') {
            node.onclick(); 
        }
    }
    

    From this post

提交回复
热议问题