此文章不写具体实现方法,只提供思路。
实现目标:
首先是某网站存在一个滑动验证码。然后需要滑动。不能每次滑动都一样。
首先记录人的滑动轨迹
这里参考自博客
你滑动鼠标,下面会生成坐标轨迹列表。
具体代码为:
<!doctype html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>log_tracks</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
*{
margin:0;
padding:0;
border:0;
}
.track-monitor{
background-color:orange;
}
.track-pad{
height:200px;
background-color:green;
}
.track-coordinate{
background-color:purple;
color:white;
height:25px;
line-height:25px;
font-size:12px;
}
.track-coordinate-list{
font-size:12px;
width:100%;
word-break:break-word;
}
</style>
<script>
window.addEventListener('load',function(){
var pad = document.getElementsByClassName('track-pad')[0];
var monitor = document.getElementsByClassName('track-monitor')[0];
var coordinate = document.getElementsByClassName('track-coordinate')[0];
var clist = document.getElementsByClassName('track-coordinate-list')[0];
var reset = document.getElementsByTagName('button')[0];
var context = monitor.getContext('2d');
var cset = [];
var startx = 0, starty = 0;
$('div').mousedown(mouseState).mouseup(mouseState);
function fixSize(){monitor.width = window.innerWidth;};
function log(e){
if(cset.length == 0){
context.moveTo(e.x,e.y);
}else{
context.strokeStyle = 'white';
context.lineTo(e.x,e.y);
context.stroke();
}
if(e.x-startx == e.x && e.y-starty == e.y){
startx = e.x;
starty = e.y;
}
coordinate.innerHTML = '(' + (e.x-startx)+', '+(e.y-starty) + ')';
cset.push(coordinate.innerHTML);
clist.innerHTML = cset.join(', ');
}
function mouseState(e) {
if (e.type == "mouseup") {
$('#logs').append('<br/>'+cset.join(', '));
clist.innerHTML = cset.join('');
cset = [];
pad.removeEventListener("mousemove", log);
}
if (e.type == "mousedown") {
startx = 0; starty = 0;
pad.addEventListener('mousemove',log);
}
}
reset.addEventListener('click',function(){
fixSize();
cset = [];
clist.innerHTML = '';
coordinate.innerHTML='在绿色的方块中滑动鼠标';
});
fixSize();
});
</script>
</head>
<body>
<div class="stage">
<div class="track-pad"></div>
<canvas width="100" height="200" class="track-monitor"></canvas>
<div class="track-coordinate">在绿色的方块中滑动鼠标</div>
<button>重置</button>
<div>
<div id="logs"></div>
<div class="track-coordinate-list"></div>
</div>
</div>
</body>
</html>
得到轨迹后模拟移动
代码实现为(自己思考,然后修改,不能直接运行):
from selenium.webdriver import ActionChains
from selenium import webdriver
result = [(0, 0), (1, 0), (6, 3), (52, 30), (62, 36), (67, 40), (71, 41), (72, 42), (73, 42), (74, 42), (78, 43), (78, 43)]
action = ActionChains(启动的驱动对象)
# 拖动滑块
action.click_and_hold(滑动的那个控件对象) # 对象通过driver.find_element_by_xpath()查找
for x in result:
action.move_by_offset(xoffset=x[0], yoffset=x[1])
time.sleep(0.5)
action.release(drag_element).perform() # 释放按钮
然后能干的事。。
记录一堆轨迹放到数据库里面,用的时候随机抽取一个用,防封!