selenium 对滑动验证框的处理
在登录的时候,有的系统会采用滑动验证的方式,今天贴下这里的代码自己学习。
使用selenium 行为事件方法ActionChains 方法,把我么要滑动的动作放进列表,然后交给selenium执行,具体的算法是其他大佬搞出来的
经过本人验证,可以识别
http://guapier.net/2019/04/12/aliyun/
import time import random from selenium import webdriver from selenium.webdriver import ActionChains url = "xxxxx" user = "xxxxxx" pas = "123123" def get_track(distance): # distance为传入的总距离 # 移动轨迹 track = [] # 当前位移 current = 0 # 减速阈值 mid = distance*4/5 # 计算间隔 t = 0.2 # 初速度 v = 1 while current < distance: if current < mid: # 加速度为2 a = random.uniform(0, 2) else: # 加速度为-2 a = random.uniform(-1, -2) v0 = v # 当前速度 v = v0 + a*t # 移动距离 move = v0*t+1/2*a*t*t # 当前位移 current += move # 加入轨迹 track.append(round(move)) return track def move_to_gap(driver, slider,tracks): # slider是要移动的滑块,tracks是要传入的移动轨迹 ActionChains(driver).click_and_hold(slider).perform() for x in tracks: ActionChains(driver).move_by_offset(xoffset=x, yoffset=0).perform() time.sleep(0.5) ActionChains(driver).release().perform() def login(): driver = webdriver.Chrome() driver.get(url) time.sleep(5) driver.find_element_by_xpath('xxxxx').send_keys(user) driver.find_element_by_xpath('xxxxx').send_keys(pas) huakuai = driver.find_element_by_xpath('xxxxx') time.sleep(5) move_to_gap(driver, huakuai, get_track(340)) time.sleep(5) driver.find_element_by_xpath('xxxxxxx').click() time.sleep(60) if __name__ == "__main__": login()
学习最大的乐趣在于分享,我是绝世老中医,欢迎来诊
个人qq:1978529954