问题
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?
回答1:
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");
回答2:
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);
回答3:
Your class seems to be missing the .
.
Try one of these:
$(".btn").click();
$("button").click();
$("input:submit").click();
$(".btn.small.info").click();
回答4:
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
.
回答5:
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:
- http://www.tutorialspoint.com/jquery/jquery-selectors.htm
- http://www.w3schools.com/jquery/jquery_ref_selectors.asp
来源:https://stackoverflow.com/questions/17300364/programmatically-clicking-all-buttons-on-a-page-in-chromes-console