allprog
allprog

Reputation: 16780

Programmatically clicking all buttons on a page in Chrome's console

I'm trying to use the Chrome console to select all the buttons in a page and click them programmatically. A similar page to what I'm playing with is this one: http://api.openstack.org/api-ref.html#compute-ext

I already tried to execute the command below but it didn't do what I wanted.

$("btn small info").click()

Is this possible at all? What command should I issue?

Upvotes: 13

Views: 28575

Answers (6)

ASin
ASin

Reputation: 106

[Simple solution for newbies] Use below to click on all the matched element

var btns = document.getElementsByClassName('followBtn')
for (var i =0; i<btns.length; i++) 
btns[i].click();

Can be used with tag names as well.

Or you can also use Xpaths to define specific elements Use below code sample

*//Xpath to get all the elements whose class is "followBtn" and tagname button*
var btns = $x('//button[@class="followBtn"]')

*// looping through all the elements in btns*
for (var i =0; i<btns.length; i++) 
btns[i].click();

Upvotes: 4

epascarello
epascarello

Reputation: 207501

Well, you want to make sure you only select the buttons in the section so you are not running the search.

$("#body .btn").trigger("click");

Upvotes: 14

mekwall
mekwall

Reputation: 28974

Based on Salketers comment to the question, here's a little script that will programatically click all buttons one by one at a 1 second interval, and also log the clicked button to the console:

var buttons = $("button"), 
    interval = setInterval(function(){
        var btn = $(buttons.splice(0, 1));
        console.log("Clicking:", btn);
        btn.click();
        if (buttons.length === 0) {
            clearInterval(interval);
        }
    }, 1000);

Upvotes: 6

stackptr
stackptr

Reputation: 177

Assuming the page already has jQuery libraries included (which the referenced page does), then:

$(".btn.small.info").click();

This will implicitly iterate through all selectors with those three classes, and simulate a click event.

If the page does not have the necessary jQuery libraries, then try this before executing the command above:

var jq = document.createElement('script');
jq.src = "http://code.jquery.com/jquery-latest.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
jQuery.noConflict();

Note that the command you're using in your question will not select elements of class btn etc. You must use a dot to select a .class.

Upvotes: 6

JasonM
JasonM

Reputation: 761

Your selector may be wrong. Try doing something like this

$(".btn.small.info").click();

That will click a button with the classes btn small info, what you had was trying to click dom elements.

Here is more documentation on jQuery selectors:

Upvotes: 4

Wouter van der Houven
Wouter van der Houven

Reputation: 448

Your class seems to be missing the .. Try one of these:

$(".btn").click();
$("button").click();
$("input:submit").click();
$(".btn.small.info").click();

Upvotes: 5

Related Questions