希望自动化以下操作:
打开网页后点击”一键展开”按钮
滚动页面到底部
点击下一页按钮
重复上述步骤
网页自动化
简单易用 : Tampermonkey 脚本最简单,无需安装额外软件
功能强大 : Selenium 或 Puppeteer 提供更多控制和自动化选项
下面使用Tampermonkey
1,添加新脚本
点击浏览器工具栏中的Tampermonkey图标
从下拉菜单中选择”添加新脚本”或”创建新脚本”
编写脚本
点击编辑器右上角的”文件”菜单
选择”保存”选项(或按Ctrl+S)
如何测试css选择器是否正确。
切换到”控制台”(Console)选项卡
输入以下代码测试您的选择器:
1
2
3
4
// 测试展开按钮选择器
var expandButton = document.querySelector('您的expandButtonSelector');
console.log('展开按钮:', expandButton);
如果不正确,将显示 null 。如果选择器正确,控制台会显示对应的DOM元素
需要 输入edge://extensions/ 在左侧找到”开发人员模式”开关并打开
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// ==UserScript==
// @name 问题列表自动展开与翻页
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 自动点击一键展开按钮并翻页
// @author You
// @match https://voc.xxx.cn/uvm/insight/voice-summary-list
// @grant none
// ==/UserScript==
// 添加一个立即执行的测试代码
alert('Tampermonkey脚本已加载');
(function() {
'use strict';
// 配置参数
const config = {
// 一键展开按钮的选择器,根据实际情况修改
expandButtonSelector: '#app > div.uvm-layout > div.uvm-right-container > div > div.el-scrollbar__wrap.el-scrollbar__wrap--hidden-default > div > div > div:nth-child(3) > div.voice-summary > div.summary-title-wrapper > div.operation-box > div > div:nth-child(3) > button > span',
// 下一页按钮的选择器,根据实际情况修改
nextPageSelector: '#app > div.uvm-layout > div.uvm-right-container > div > div.el-scrollbar__wrap.el-scrollbar__wrap--hidden-default > div > div > div:nth-child(3) > div.voice-summary > div.voice-summary-footer > div:nth-child(1) > button.btn-next.is-last',
// 列表项的选择器,用于检测列表是否已加载
listItemSelector: '#app > div.uvm-layout > div.uvm-right-container > div > div.el-scrollbar__wrap.el-scrollbar__wrap--hidden-default > div > div > div:nth-child(3) > div.voice-summary > div.voice-summary-body > div > div.el-table__inner-wrapper > div.el-table__body-wrapper > div > div.el-scrollbar__wrap.el-scrollbar__wrap--hidden-default > div > table > tbody > tr:nth-child(1) > td > div > div',
// 等待展开内容的时间(毫秒)
waitTimeAfterExpand: 3000,
// 是否在页面底部添加控制按钮
addControlButtons: true,
// 控制按钮显示延迟时间(毫秒)
controlButtonDelay: 1000,
// 最大翻页次数
maxPageCount: 3
};
// 添加一个变量来跟踪当前页数
let currentPageCount = 0;
// 滚动到页面底部
function scrollToBottom() {
// 找到实际的滚动容器
const scrollContainer = document.querySelector('#app > div.uvm-layout > div.uvm-right-container > div > div.el-scrollbar__wrap.el-scrollbar__wrap--hidden-default');
if (scrollContainer) {
scrollContainer.scrollTo({
top: scrollContainer.scrollHeight,
behavior: 'smooth'
});
} else {
// 如果找不到特定容器,则滚动整个页面
window.scrollTo({
top: document.body.scrollHeight,
behavior: 'smooth'
});
}
}
// 查找下一页按钮
function findNextPageButton() {
// 尝试多种可能的下一页按钮选择器
const nextPageButton = document.querySelector(config.nextPageSelector);
// 如果找不到,尝试查找包含">"文本的链接
if (!nextPageButton) {
const allLinks = document.querySelectorAll('a');
for (const link of allLinks) {
if (link.textContent.includes('>') || link.textContent.includes('下一页')) {
return link;
}
}
}
return nextPageButton;
}
// 添加控制按钮
function addControls() {
const controlPanel = document.createElement('div');
controlPanel.style.position = 'fixed';
controlPanel.style.bottom = '20px';
controlPanel.style.right = '20px';
controlPanel.style.zIndex = '9999';
controlPanel.style.background = '#fff';
controlPanel.style.padding = '10px';
controlPanel.style.border = '1px solid #ccc';
controlPanel.style.borderRadius = '5px';
const startButton = document.createElement('button');
startButton.textContent = '开始自动操作';
startButton.style.marginRight = '10px';
startButton.onclick = processPage;
controlPanel.appendChild(startButton);
document.body.appendChild(controlPanel);
console.log('elesos 控制按钮已添加到页面');
}
// 等待列表项加载
function waitForListItems(isNextPage = false) {
console.log('elesos 开始等待列表项加载...');
// 创建一个Promise来处理异步等待
return new Promise((resolve) => {
function checkAndWait() {
// 使用MutationObserver监听整个文档的变化
const observer = new MutationObserver((mutations, obs) => {
const listItems = document.querySelectorAll(config.listItemSelector);
if (listItems && listItems.length > 0) {
console.log('elesos MutationObserver 检测到列表数据已加载');
obs.disconnect(); // 停止观察
resolve();
if (isNextPage) {
processPage();
} else {
startAddControl();
}
}
});
// 配置观察选项,改为观察整个文档
observer.observe(document.body, {
childList: true, // 观察子节点变化
subtree: true // 观察所有后代节点
});
}
// 开始检查
checkAndWait();
});
}
// 主要处理流程
async function processPage() {
console.log('elesos 开始处理当前页面...');
// 检查是否达到最大页数限制
if (config.maxPageCount > 0 && currentPageCount >= config.maxPageCount) {
console.log('elesos 已达到最大页数限制,停止处理');
return;
}
// 1. 点击一键展开按钮
let retryCount = 0;
const maxRetries = 5;
while (retryCount < maxRetries) {
const expandButton = document.querySelector(config.expandButtonSelector);
if (expandButton) {
console.log('elesos 找到一键展开按钮,等待3秒后开始点击');
await new Promise(resolve => setTimeout(resolve, 3000)); // 添加3秒延迟
console.log('elesos 开始点击一键展开按钮');
expandButton.click();
break;
} else {
console.log(`elesos 未找到一键展开按钮,等待后重试 (${retryCount + 1}/${maxRetries})`);
await new Promise(resolve => setTimeout(resolve, 2000));
retryCount++;
}
}
// 等待内容展开
await new Promise(resolve => setTimeout(resolve, config.waitTimeAfterExpand));
// 2. 滚动到页面底部
console.log('elesos 滚动到页面底部');
scrollToBottom();
// 等待滚动完成
await new Promise(resolve => setTimeout(resolve, 5000));
// 3. 点击下一页
const nextPageButton = findNextPageButton();
if (nextPageButton) {
currentPageCount++; // 增加页数计数
console.log(`elesos 点击下一页按钮 当前页数: ${currentPageCount}/${config.maxPageCount}`);
nextPageButton.click();
// 点击下一页后等待新页面加载
await waitForListItems(true);
} else {
console.log('elesos 未找到下一页按钮或已到最后一页');
}
}
function startAddControl() {
if (config.addControlButtons) {
setTimeout(addControls, config.controlButtonDelay);
}
}
// 初始化
function init() {
try {
waitForListItems();
} catch (error) {
console.error('elesos 初始化出错:', error);
}
}
// 页面完全加载后初始化(包括所有资源)
window.onload = function() {
init();
};
})();
开发中遇到2个问题
1是一键展开按钮需要等待列表数据加载完才出现,需要延时3秒再点
2是滚动条div,直接通过浏览器复制的不行。
网页的滚动条div是
不是复制的这个
需要改。