此文章不寫具體實(shí)現(xiàn)方法白对,只提供思路掠廓。
實(shí)現(xiàn)目標(biāo):
首先是某網(wǎng)站存在一個(gè)滑動(dòng)驗(yàn)證碼。然后需要滑動(dòng)甩恼。不能每次滑動(dòng)都一樣蟀瞧。
首先記錄人的滑動(dòng)軌跡
這里參考自博客
你滑動(dòng)鼠標(biāo)沉颂,下面會(huì)生成坐標(biāo)軌跡列表。
具體代碼為:
<!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='在綠色的方塊中滑動(dòng)鼠標(biāo)';
});
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">在綠色的方塊中滑動(dòng)鼠標(biāo)</div>
<button>重置</button>
<div>
<div id="logs"></div>
<div class="track-coordinate-list"></div>
</div>
</div>
</body>
</html>
得到軌跡后模擬移動(dòng)
代碼實(shí)現(xiàn)為(自己思考悦污,然后修改铸屉,不能直接運(yùn)行):
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(啟動(dòng)的驅(qū)動(dòng)對(duì)象)
# 拖動(dòng)滑塊
action.click_and_hold(滑動(dòng)的那個(gè)控件對(duì)象) # 對(duì)象通過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() # 釋放按鈕
然后能干的事。切端。
記錄一堆軌跡放到數(shù)據(jù)庫(kù)里面彻坛,用的時(shí)候隨機(jī)抽取一個(gè)用,防封踏枣!