How to programmatically press a button through the browser console [code inside]

For some reason, I can click some links / buttons, but I can’t use the buttons / everything that has the onclick attribute. For example: my JavaScript code that I entered into the browser console:

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

I put console.log so that I know what the browser is doing and where it is now.

Html code per page:

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

Basically, I want to take pictures quickly using the browser console, but when I enter my code, I get this error:

TypeError: object # does not have a 'click' method

When I use the same code, say to add friends on facebook again, 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 to no avail.

+3
source share
2

:

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

o . o[i] i.

+4

, . document.getElementsByName(), . . , .

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);
    }
}
+5

All Articles