Logo Questions Linux Laravel Mysql Ubuntu Git Menu

How does one programmatically click a button via the browser console [code inside]

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++){

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

The html code on the page:

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

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++){

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

like image 892
Jeromie Devera Avatar asked Dec 25 '22 13:12

Jeromie Devera

1 Answers

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++){
like image 109
Bic Avatar answered Jan 05 '23 19:01