利用Chrome开发者控制台尝试微博自动点赞评论
设置了定时,但还是运行很快
应该怎样等待点击评论按钮后并出现评论输入框后输入评论内容呢
还有应该怎样写这种定时语句呢,谢谢
function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
let reg = /关键词/g;
let wrapper = document.querySelector("#Pl_Core_MixedFeed__262 > div > div.WB_feed.WB_feed_v3.WB_feed_v4");
let commentNum = 0;
for (let i = 0; i < 10; i++) {
// 第i+1楼 div 标签
let test = wrapper.children[i];
try {
let str = test.querySelector('div>div:nth-child(3)>div:nth-child(4)').innerText.toString();
// 匹配关键字
if (reg.exec(str)) {
// 获取 点赞和评论
let good = wrapper.children[i].querySelector('.WB_feed_handle>div>ul>li:nth-child(4) a');
let comment = wrapper.children[i].querySelector('.WB_feed_handle>div>ul>li:nth-child(3) a');
// 点赞 点击评论按钮
good.click();
// 点击评论按钮
sleep(3000).then(() => {
comment.click();
});sleep
// 写评论
try{
sleep(3000).then(() => {
wrapper.children[i].querySelector('div.WB_feed_repeat.S_bg1>div>div textarea').textContent = '评论内容';
});
// 提交评论
//sleep(3000).then(() => {
// wrapper.children[i].querySelector('div.WB_feed_repeat.S_bg1>div>div>div>div>div a').click();
//});
sleep(3000).then(() => {
comment.click();
console.log('第' + i + '楼匹配评论成功');
});
commentNum++;
}catch{
console.log('找不到textContent');
}
}
}
catch {
console.log(i + '楼,我也不知道什么错误');
}
}