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

javascript - How does one programmatically click a button via the browser console [code inside] - Stack Overflow

programmeradmin2浏览0评论

For some reason, I am able to click some links/buttons, but cannot for buttons/anything that has an onclick attribute. For example: my JavaScript code I input into the browser's console:

var o = document.getElementsByName("takepic"); 
for (var i = 0; i < 1000; i++){
    o.click();
    console.log(i);
}

I put console.log so I know what the browser is doing, and where it currently is at.

The html code on the page:

<form>
    <input type="button" value="Configure..." onclick="webcam.configure()">
    &nbsp;&nbsp;
    <input type="button" value="Take Snapshot" onclick="take_snapshot()" name="takepic">
</form>

So basically, I want to take rapid snapshots using the browser console, but when I enter in my code, I get this error:

TypeError: Object # has no method 'click'

When I do use the same code, say for re-adding friends on facebook, and I use this:

var o = document.getElementsByName("fbaddfriend_example"); 
for (var i = 0; i < o.length; i++){
    o[i].click(); 
    console.log(i);
}

It definitely works. I'm just trying to do the same with a button on a page, but with no avail.

For some reason, I am able to click some links/buttons, but cannot for buttons/anything that has an onclick attribute. For example: my JavaScript code I input into the browser's console:

var o = document.getElementsByName("takepic"); 
for (var i = 0; i < 1000; i++){
    o.click();
    console.log(i);
}

I put console.log so I know what the browser is doing, and where it currently is at.

The html code on the page:

<form>
    <input type="button" value="Configure..." onclick="webcam.configure()">
    &nbsp;&nbsp;
    <input type="button" value="Take Snapshot" onclick="take_snapshot()" name="takepic">
</form>

So basically, I want to take rapid snapshots using the browser console, but when I enter in my code, I get this error:

TypeError: Object # has no method 'click'

When I do use the same code, say for re-adding friends on facebook, and I use this:

var o = document.getElementsByName("fbaddfriend_example"); 
for (var i = 0; i < o.length; i++){
    o[i].click(); 
    console.log(i);
}

It definitely works. I'm just trying to do the same with a button on a page, but with no avail.

Share Improve this question edited Feb 23, 2014 at 18:30 j08691 208k32 gold badges268 silver badges280 bronze badges asked Feb 23, 2014 at 18:21 Jeromie DeveraJeromie Devera 3762 gold badges6 silver badges22 bronze badges 2
  • Yup, but for some reason SOF puts them in separate lines [which is pretty cool]. – Jeromie Devera Commented Feb 23, 2014 at 18:33
  • Some element types do have a click method, some don't. See duplicate of How to simulate mouse click using Javascript? – Bergi Commented Feb 23, 2014 at 18:42
Add a comment  | 

2 Answers 2

Reset to default 10

Your problem is in the code you're typing into the console. You are using document.getElementsByName(), which will return an array of elements. You need to loop through those elements. You are doing this in your second code segment, but not your first.

var o = document.getElementsByName("takepic"); 
for (var j = 0; j < o.length; j++) {
    for (var i = 0; i < 1000; i++){
        o[j].click();
        console.log(i);
    }
}

Change the code to:

var o = document.getElementsByName("takepic"); 
for (var i = 0; i < 1000; i++){
// ---v
    o[i].click();
    console.log(i);
}

The variable o holds a collection of all retrieved elements. o[i] returns the element at position i.

发布评论

评论列表(0)

  1. 暂无评论