简体   繁体   English

如何通过浏览器控制台以编程方式单击按钮[代码内部]

[英]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. 出于某种原因,我可以单击某些链接/按钮,但不能用于具有onclick属性的按钮/任何内容。 For example: my JavaScript code I input into the browser's console: 例如:我输入浏览器控制台的JavaScript代码:

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

I put console.log so I know what the browser is doing, and where it currently is at. 我把console.log放进去,所以我知道浏览器正在做什么,以及它目前在哪里。

The html code on the page: 页面上的html代码:

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

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' TypeError:Object#没有方法'click'

When I do use the same code, say for re-adding friends on facebook, and I use this: 当我使用相同的代码时,比如在Facebook上重新添加朋友,我使用这个:

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 with no avail. 我只是试图用页面上的按钮做同样的事,但没有用。

Your problem is in the code you're typing into the console. 您的问题出在您在控制台中输入的代码中。 You are using document.getElementsByName() , which will return an array of elements. 您正在使用document.getElementsByName() ,它将返回一个元素数组。 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++){
        o[j].click();
        console.log(i);
    }
}

Change the code to: 将代码更改为:

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

The variable o holds a collection of all retrieved elements. 变量o包含所有检索元素的集合。 o[i] returns the element at position i . o[i]返回位置i的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过浏览器中的控制台单击使用 javascript 的按钮 - Click a button using javascript via console in browser 如何在浏览器控制台中按名称单击按钮 - How to Click a Button by Name in Browser Console 能否检测到在控制台浏览器中运行的JavaScript代码 - Can one detect javascript code run inside console browser click()函数不在任何浏览器控制台上 - click() function does not on any browser console 如何以编程方式打开浏览器的 JavaScript 控制台? - How to open browser's JavaScript console programmatically? 如何在单击按钮 HTML 代码上关闭浏览器? - How do I close a browser on click button HTML code? 如何使用 gmail 中的控制台以编程方式单击下一个按钮以查看旧邮件? - How can I click on the next button programmatically using console in gmail to view older mails? 如何单击一个按钮,监听该单击,如果在特定浏览器上...添加另一个程序化单击? - How to click one button, listen for that click, and if on a specific browser... add another programmatic click? 点击按钮并在其他浏览器的iframe中导航 - Click a button and navigate inside an iframe of different browser 当.click()不起作用时,如何以编程方式单击此页面上的按钮? - How do I programmatically click a button on this page when .click() does not work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM