puppeteer 自动点击
基于puppeteer的。 做了半天好像也和按键精灵没什么区别。。。 还是贴代码吧。
.npmrc
{
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD = true
}
- 用于跳过puppeteer的默认chromium安装,改成手动安装,亲测,即使用了ss翻墙了,依然不能成功安装
- 手动安装 chrome canary https://www.google.com/chrome/browser/canary.html (目测比chromium更好使)
自动点击打开网页的发送短信
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
executablePath: "C:/Users/33318/AppData/Local/Google/Chrome SxS/Application/chrome.exe",
timeout: 0,
//将默认无头模式 ----> 有头模式
headless: false,
// 每次type输入字母/间隔100m
slowMo: 100
});
const page = await browser.newPage();
//字面意思,你要chrome打开的网页
await page.goto('what url you want');
// debug tools
await page.evaluate(() => {
//页面入侵,可在打开的前台页面console 界面键入代码,更改dom信息,。。。
window.addEventListener('mousemove', e => {
try {
//添加点击动画,留下点击痕迹,便于测试
const div = document.createElement('div');
div.style.width = '5px';
div.style.height = '5px';
div.style.borderRadius = '50%';
div.style.backgroundColor = 'red';
div.style.position = 'absolute';
div.style.left = e.x + 5 + 'px';
div.style.top = e.y + 5 + 'px';
div.style.zIndex = "99999";
document.body.appendChild(div);
} catch (err) {
console.error(err);
}
});
});
let login = await page.$('input[name=phone]')
await login.type(phone)
let button = await page.$('.login-reg-right')
await button.click()
await page.deleteCookie();
await browser.close();
})();
gitter聊天室自动登录github 发送聊天信息,撤回聊天信息,无限循环
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
executablePath: "C:/Users/33318/AppData/Local/Google/Chrome SxS/Application/chrome.exe",
timeout: 0,
headless: false,
slowMo: 100
});
const page = await browser.newPage();
await page.goto('what url you want');
await page.goto(e.url);
// debug tools
await page.setViewport({
width :1600,
//设置成0,默认界面最大化
height :0
})
// //gitter page
const iframe = await page.frames().find(f => f.name() === 'content-frame');
await iframe.evaluate(()=>{
window.addEventListener('mousemove', e => {
try {
const div = document.createElement('div');
div.style.width = '5px';
div.style.height = '5px';
div.style.borderRadius = '50%';
div.style.backgroundColor = 'red';
div.style.position = 'absolute';
div.style.left = e.x + 5 + 'px';
div.style.top = e.y + 5 + 'px';
div.style.zIndex = "99999";
document.body.appendChild(div);
} catch (err) {
console.error(err);
}
});
})
const button = await iframe.$('footer a')
await button.click();
let github = await iframe.$('.login-view__buttons > a.button-github--small')
await github.click('top')
// //github page
await page.waitFor('input[name=login]')
await page.waitFor('input[name=password]')
let name = await page.$('input[name=login]')
let password = await page.$('input[name=password]')
await name.type('penglih');
await password.type('ewqewq123');
await page.keyboard.press('Enter');
//back to gitter
await page.setViewport({
width :1600,
height :0
})
// type puppeteer
await page.waitFor('#content-frame')
await sleep(1000)
const frame = await page.frames().find(f => f.name() === 'content-frame');
await frame.evaluate(()=>{
window.addEventListener('mousemove', e => {
try {
const div = document.createElement('div');
div.style.width = '5px';
div.style.height = '5px';
div.style.borderRadius = '50%';
div.style.backgroundColor = 'red';
div.style.position = 'absolute';
div.style.left = e.x + 5 + 'px';
div.style.top = e.y + 5 + 'px';
div.style.zIndex = "99999";
document.body.appendChild(div);
setTimeout(() => {
div.remove()
}, 1000);
} catch (err) {
console.error(err);
}
});
})
await frame.waitFor('#chat-container .chat-item')
let i = 0;
while(true){
i++
let textarea = await frame.$$('textarea');
await textarea[0].type(`${i} (node:5368) UnhandledProm UnhandledProm`);
await sleep(30)
await page.keyboard.down('Control');
await sleep(30)
await page.keyboard.press('Enter');
await sleep(10)
await page.keyboard.up('Control');
await sleep(1999)
//获取可删除的按钮数组
let reback = await frame.$$('#chat-container .isViewers .icon-ellipsis');
await reback[0].click('middle')
await sleep(2000)
await frame.waitFor('.js-chat-action-delete');
let deleteMsg = await frame.$$('.js-chat-action-delete');
await deleteMsg[0].click('middle')
await sleep(2001)
}
})();
为了让点击更接近于人工点击,sleep()函数是必不可少的
utils/sleep.js
class sleep{
async sleep(time){
console.log(`now to sleep ${time}ms`);
return new Promise( next=> {
setTimeout(()=> {
next();
}, time);
})
};
}
module.exports = new sleep();
2 回复
挺好的~!
我们的 cnpm 有对这个库做了加速了,可以用 cnpm 来安装