0%

使用 JS 批量点击按钮

打开网页,Ctrl+Shift+C 调出开发者工具,点选需要按下的按钮或元素,查看 html 源码,找到多个元素的相同点
例如:

1
2
3
<button aria-label="按钮 1" type="button" class="Button VoteButton VoteButton--up">按钮 1</button>
<button aria-label="按钮 2" type="button" class="Button VoteButton VoteButton--up">按钮 2</button>
<button aria-label="按钮 3" type="button" class="Button VoteButton VoteButton--up">按钮 3</button>

可以看出,这些按钮的 class 是相同的,都是”Button VoteButton VoteButton–up”

打开 Console,输入:

1
document.getElementsByClassName("Button VoteButton VoteButton--up")

可以看到返回了一堆结果

接下来开始测试点击效果

1
document.getElementsByClassName("Button VoteButton VoteButton--up")[0].click()

如果没有意外,第一个按钮的点击事件已经被触发了

那么开始写批量脚本:

1
2
3
4
var buttons = document.getElementsByClassName("Button VoteButton VoteButton--up");
for (var i = 0; i < buttons.length; i++) {
buttons[i].click();
}

或者

1
2
3
while(document.getElementsByClassName("Button VoteButton VoteButton--up")[0] != null){
document.getElementsByClassName("Button VoteButton VoteButton--up")[0].click();
}

这就完成了,可以把页面上所有按钮都点一遍!