简体   繁体   English

如何在Chrome控制台中单击网页中的按钮?

[英]how to click a button in a webpage from chrome console?

I used 我用了

Method 1: $("button").click(); 方法1: $("button").click();
Result: Cannot read property 'click' of null 结果: Cannot read property 'click' of null

Method 2: $("button").trigger("click"); 方法2: $("button").trigger("click");
Result: Cannot read property 'trigger' of null 结果: Cannot read property 'trigger' of null

If your page doesn't reference jQuery, you can simply find the element by its ID using javascript and attach click event to it as follows: 如果您的页面未引用jQuery,则可以使用javascript通过其ID查找元素,然后将click事件附加到它,如下所示:

document.getElementById('mainbar').addEventListener("click", function() 
{
    console.log('main bar clicked');
});

Using jQuery:

$('#buttonid').click();

如果ID和jQuery不可用的解决方案:

document.getElementsByTagName("button")[0].click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM