高德地图滑动验证
- 用dispatch 模拟dom的mousedown, mousemove,还有mouseUp 事件
- 再用
setInterval
执行mouseMove
事件即可 - 但这里有时候会滑动失败,所以要刷新验证码,然后重新滑动,
- 有时候又是滑动验证码验证成功后。 再一次弹出验证码
- 注意:滑动验证码请注意滑动的速度和滑动的距离,要是太慢、滑动距离太短的话,滑动验证多数会不通过。
检查验证码
// 检查验证码
function checkYzm() {
const dom = document.querySelector(".sufei-dialog");
if(dom && dom.style.display == "block") {
return true;
}
return false
}
滑动验证码
async function sliderYzm() {
await sleep(1500);
const iframe = document.getElementById("sufei-dialog-content");
const btn = iframe.contentWindow.document.getElementById("nc_1_n1z");
const mousedown = document.createEvent("MouseEvents");
const rect = btn.getBoundingClientRect();
var x = rect.x;
var y = rect.y;
mousedown.initMouseEvent("mousedown", true, true, window, 0, x, y, x, y, false, false, false, false, 0, null);
btn.dispatchEvent(mousedown);
let dx = 0;
let dy = 0;
const mousemove = document.createEvent("MouseEvents");
return new Promise((resolve) => {
const interval = setInterval(async () => {
const _x = x + dx;
const _y = y + dy;
mousemove.initMouseEvent("mousemove", true, true, window,0 ,_x, _y, _x, _y, false, false, false, false, 0, null);
btn.dispatchEvent(mousemove);
if(_x - x >= 300){
clearInterval(interval);
const mouseup = document.createEvent("MouseEvents");
mouseup.initMouseEvent("mouseup", true, true, window, 0, _x, _y, _x, _y, false, false, false, false, 0, null);
btn.dispatchEvent(mouseup);
await sleep(2500);
if(btn.className.indexOf('btn_ok') > -1){
console.log(btn.className);
sleep(1000)
resolve({status: 1});
}
if(iframe.contentWindow.document.querySelector("#nocaptcha > .errloading")) {
console.log("error")
sleep(1000)
resolve({status: 0})
}
} else{
dx += Math.ceil(Math.random() * 300);
}
}, 30);
})
}
上面只是滑动验证
循环检查时候是否有验证码
/**
* 循环检查是否有验证码
* 1. 要是有验证码窗口,那么滑动验证, 但滑动验证有失败或者成功,失败返回0, 成功返回1
* 2. 滑动成功后,防止验证码再一次出现,我们延迟1500毫秒,再查看一下验证码窗口是否存在,存在就再一次执行loopSlideYzm(), 不存在就直接结束函数了
* 3. 滑动失败后,会有刷新标志,那这时候应该点击刷新后,再一次执行loopSlideYzm();
*/
async function loopSlideYzm() {
await sleep(1200);
const hasYzm = checkYzm();
await sleep(1000);
if(hasYzm) {
const { status } = await sliderYzm();
await sleep(1500);
if(status) {
console.log("再找一次是否有验证码")
const hasYzmAgain = checkYzm();
await sleep(1500);
if(hasYzmAgain) {
return await loopSlideYzm();
} else {
return Promise.resolve()
}
} else {
console.log("给我刷新验证码");
freshYzm();
await sleep(1500);
return await loopSlideYzm();
}
}
return Promise.resolve()
}
刷新验证码
function freshYzm() {
const iframe = document.getElementById("sufei-dialog-content");
iframe.contentWindow.document.querySelector("#nocaptcha > .errloading > .nc-lang-cnt > a").click()
}
以上就是自动跳过高德地图的滑动验证码全过程