jQuery 以编程方式单击 Chrome 控制台中页面上的所有按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17300364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically clicking all buttons on a page in Chrome's console
提问by allprog
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
我正在尝试使用 Chrome 控制台选择页面中的所有按钮并以编程方式单击它们。与我正在玩的类似页面是这个: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?
这可能吗?我应该发出什么命令?
回答by epascarello
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");
回答by mekwall
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:
根据 Salketers 对该问题的评论,这里有一个小脚本,它将以编程方式以 1 秒的间隔一个接一个地单击所有按钮,并将单击的按钮记录到控制台:
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);
回答by Wouter van der Houven
Your class seems to be missing the .
.
Try one of these:
您的课程似乎缺少.
. 尝试以下方法之一:
$(".btn").click();
$("button").click();
$("input:submit").click();
$(".btn.small.info").click();
回答by Corey
Assuming the page already has jQuery libraries included (which the referenced page does), then:
假设页面已经包含 jQuery 库(引用的页面包含),那么:
$(".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:
如果页面没有必要的 jQuery 库,那么在执行上面的命令之前试试这个:
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
.
请注意,您在问题中使用的命令不会选择类 btn 等的元素。您必须使用点来选择.class
.
回答by JasonM
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.
这将点击一个带有 classes 的按钮btn small info
,你试图点击 dom 元素。
Here is more documentation on jQuery selectors:
这里有更多关于 jQuery 选择器的文档: