最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to click a button in a webpage from chrome console? - Stack Overflow

programmeradmin1浏览0评论

I used

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

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

I used

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

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

Share Improve this question edited May 16, 2018 at 6:39 Pankit Kapadia 1,57913 silver badges25 bronze badges asked May 16, 2018 at 6:16 JustCodeJustCode 6812 gold badges8 silver badges19 bronze badges 6
  • 2 Does the button have id value? Does the web page contain jquery file? – Ankit Agarwal Commented May 16, 2018 at 6:19
  • 1 Possible duplicate of stackoverflow./questions/37667139/… . If you mean it, of course. Your question is not plete. – vahdet Commented May 16, 2018 at 6:21
  • 1 In chrome console, you can use $0 to refer the selected element – Thum Choon Tat Commented May 16, 2018 at 6:22
  • 1 Try $("button")[0].click(); – Pankit Kapadia Commented May 16, 2018 at 6:32
  • 1 var intervalID; $('.button').click(function(){ intervalID = setInterval(function(){ $("._njrw0").click(); },3000); }); when i run this code in chrome console i got "cannot read property click of null" – JustCode Commented May 16, 2018 at 10:51
 |  Show 1 more ment

2 Answers 2

Reset to default 2

solution if an ID and jQuery not available:

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

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:

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

Using jQuery:

$('#buttonid').click();
发布评论

评论列表(0)

  1. 暂无评论