简单的滑动验证码处理

1、计算缺口距离

def move(self, times=5):
    '''
    破解滑动验证码
    :param times: 最大失败次数,默认5,即最多尝试滑动5次
    :return: 破解成功返回True,反之None
    '''

    # 破解验证码,默认破解5次,5次后仍然失败则返回False
    for count in range(times):
        # 计算缺口距离
        distance = 302
        # 开始滑动
        result = self._move(distance)
        if result:
            return True
        else:
            pass

2、开始滑动。

def _move(self, distance):
    '''
    滑动
    :param distance: 滑动的距离,以像素为单位,int型
    :return: 成功返回True,失败返回False
    '''

    # 获取初始滑动距离列表
    tarck_list = self._get_tarck(distance)
    time.sleep(1)
    # 第一步,点击滑动按钮
    element = self.wait.until(EC.presence_of_element_located((By.XPATH, '')))
    ActionChains(self.browser).click_and_hold(element).perform()

    num = 0
    length = 265  #以滑块右端到最右侧的像素为标准

    #当num等于length时候,跳出循环,也就是滑块拉到最右端
    while True:
        xoffset = random.randint(1, 80)
        num += xoffset
        if num > length:
            xoffset = num - length
            ActionChains(self.browser).move_by_offset(xoffset=xoffset, yoffset=0).perform()
            time.sleep(random.random())
            break
        else:
            ActionChains(self.browser).move_by_offset(xoffset=xoffset, yoffset=0).perform()
            time.sleep(random.random())

    time.sleep(1)
    # 第三步,释放鼠标
    ActionChains(self.browser).release(on_element=element).perform()

    try:
        #判断滑块是否拉到最右端
        element = self.wait.until(
            EC.presence_of_element_located((By.XPATH, '')))
        return True

    except Exception as e:
        print('--------拉动滑块的过程出错---------')

3、生成滑动距离的列表。

def _get_tarck(self, distance):
    '''
    生成滑动距离列表
    :param distance: 滑动的距离,以像素为单位,int型
    :return: 返回滑动距离列表
    '''
    track = []
    # 当前位移
    current = 0
    # 什么时候减速
    mid = distance * 4 / 5
    # 计算间隔
    t = 0.2
    # # 初速度
    v = 0
    while current < distance:
        if current < mid:
            a = 2  # 加速度
        else:
            a = -3  #
        # 初速度
        v0 = v
        # 当前速度
        v = v0 + a * t
        # 移动距离
        move = v0 * t + 0.5 * a * t * t
        current += move
        track.append(round(move))

    for i in range(5):
        track.append(-1)
    return track

猜你喜欢

转载自blog.csdn.net/qq_39138295/article/details/88315176