[Python_Selenium Study Notes (8)] Implementing slider verification code cracking based on Selenium module

Preface

Some website pages will perform slider verification when accessed. To address this problem, this article will introduce how to crack the slider verification code based on the Selenium module, and take the example of simulating the login to Douban and cracking its slider verification code. explain.

text

1. Principle of cracking slider verification code

Use the Selenium module to completely simulate human behavior, hold down the slider, and move to the gap position.

2. Steps to crack slider verification code

  1. First, move the slider to a certain position quickly at one time, and then divide the distance from the slider position to the gap position into five equal distances.
  2. The first 4/5 of the distance slides quickly
  3. For the last 1/5 of the distance, first move at a uniform speed, then move at a uniform speed, and finally reach the gap.
    Note: The above steps completely simulate human behavior. If you slide too fast, it will be recognized by the website as a crawler program.

3. Slider verification code cracking-acceleration function

For the first 4/5 of the distance in steps 2 and 3, slide quickly, and the last 1/5 of the distance will first move at a uniform acceleration, and then move at a uniform deceleration, and finally reach the gap position. An acceleration function is needed to realize this function.

# 加速度函数
def get_tracks(distance):
    """
    拿到移动轨迹,模拟人的滑动行为,先匀加速后匀减速
    匀变速运动基本公式:
    1.v=v₀+at
    2.s=v₀t+1/2*at²
    :param distance:滑块一次性快速移动到某一位置后剩下的距离,进行五等分
    :return:位置/轨迹列表,列表内的一个元素代表0.3s的位移
    """
    v = 0  # 初速度
    t = 0.3  # 单位时间为0.3s来统计轨迹,轨迹即0.3内的位移
    tracks = []  # 位置/轨迹列表,列表内的一个元素代表0.3s的位移
    current = 0  # 当前的位移
    mid = distance * 4 / 5  # 到达mid值开始减速,前4/5匀加速,后1/5匀减速
    while current < distance:
        if current < mid:  # 加速度越小,单位时间内的位移越小,模拟的轨迹就越多越详细
            a = 2
        else:
            a = -3
        v0 = v  # 初速度
        s = v0 * t + 0.5 * a * (t ** 2)  # 0.3s内的位移
        current += s  # 当前的位置
        tracks.append(round(s))  # 添加到轨迹列表
        v = v0 + a * t  # 速度已经达到v,该速度作为下次的初速度
    return tracks  # tracks:[第1个0.3s的移动距离,第2个0.3s的移动距离,......]

4. Slider verification code cracking case

  1. Case requirements: After entering the username and password on the Douban.com login interface, enter the slider verification interface and use Selenium+Chrome to locate and move the node.

  2. URL address: https://www.douban.com/

  3. How to enter the slider verification interface, please refer to: [Python_Selenium Study Notes (7)] Implementing frame switching based on the Selenium module

  4. Switch to slider validation iframe subpage:'//*[@id="tcaptcha_iframe_dy"]'Insert image description here

  5. Press and hold the Start Slide Position button - move 180 pixels first:

    start_node = driver.find_element(By.XPATH, '//*[@id="tcOperation"]/div[6]')
    ActionChains(driver).click_and_hold(on_element=start_node).perform()  # 点击并按住
    ActionChains(driver).move_to_element_with_offset(to_element=start_node, xoffset=145,
                                                     yoffset=0).perform()  # 移动到距离某一节点多少距离的位置
    time.sleep(1)
    
  6. Use the acceleration function to move the remaining distance:

    tracks = get_tracks(45)
    for track in tracks:
        ActionChains(driver).move_by_offset(xoffset=track, yoffset=0).perform()
        # move_by_offset:鼠标从当前位置移动多少的距离
    
  7. Delay mouse release: release()

    time.sleep(1)
    ActionChains(driver).release().perform()
    
  8. Complete code:

    import time
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    
    
    # 加速度函数
    def get_tracks(distance):
        """
        拿到移动轨迹,模拟人的滑动行为,先匀加速后匀减速
        匀变速运动基本公式:
        1.v=v₀+at
        2.s=v₀t+1/2*at²
        :param distance:滑块一次性快速移动到某一位置后剩下的距离,进行五等分
        :return:位置/轨迹列表,列表内的一个元素代表0.3s的位移
        """
        v = 0  # 初速度
        t = 0.3  # 单位时间为0.3s来统计轨迹,轨迹即0.3内的位移
        tracks = []  # 位置/轨迹列表,列表内的一个元素代表0.3s的位移
        current = 0  # 当前的位移
        mid = distance * 4 / 5  # 到达mid值开始减速,前4/5匀加速,后1/5匀减速
        while current < distance:
            if current < mid:  # 加速度越小,单位时间内的位移越小,模拟的轨迹就越多越详细
                a = 2
            else:
                a = -3
            v0 = v  # 初速度
            s = v0 * t + 0.5 * a * (t ** 2)  # 0.3s内的位移
            current += s  # 当前的位置
            tracks.append(round(s))  # 添加到轨迹列表
            v = v0 + a * t  # 速度已经达到v,该速度作为下次的初速度
        return tracks  # tracks:[第1个0.3s的移动距离,第2个0.3s的移动距离,......]
    
    
    driver = webdriver.Chrome()  # 创建浏览器对象
    driver.get(url="https://www.douban.com/")
    
    # 1、先切换到 iframe子页面
    iframe_node = driver.find_element(By.XPATH, '//*[@id="anony-reg-new"]/div/div[1]/iframe')
    # //*[@id="anony-reg-new"]/div/div[1]/iframe
    driver.switch_to.frame(iframe_node)  # 切换到 iframe子页面
    # /html/body/div[1]
    # 2、找到密码登录 并点击
    driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/ul[1]/li[2]').click()
    
    # 3、找到找到手机号/邮箱、密码的输入框,和登录豆瓣按键
    username = driver.find_element(By.XPATH, '//*[@id="username"]')
    username.send_keys("13885124596")
    
    password = driver.find_element(By.XPATH, '//*[@id="password"]')
    password.send_keys("123456")
    
    driver.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[1]/div[5]/a').click()
    time.sleep(1)
    # 4、切换到新的iframe子页面-滑块验证
    verify_iframe = driver.find_element(By.XPATH, '//*[@id="tcaptcha_iframe_dy"]')
    # //*[@id="tcaptcha_iframe_dy"]
    driver.switch_to.frame(verify_iframe)
    # 5、按住开始滑动位置按钮 - 先移动180个像素
    start_node = driver.find_element(By.XPATH, '//*[@id="tcOperation"]/div[6]')
    ActionChains(driver).click_and_hold(on_element=start_node).perform()  # 点击并按住
    ActionChains(driver).move_to_element_with_offset(to_element=start_node, xoffset=145,
                                                     yoffset=0).perform()  # 移动到距离某一节点多少距离的位置
    time.sleep(1)
    # 6、使用加速度函数移动剩下的距离
    tracks = get_tracks(45)
    for track in tracks:
        ActionChains(driver).move_by_offset(xoffset=track, yoffset=0).perform()
        # move_by_offset:鼠标从当前位置移动多少的距离
    # 7、延迟释放鼠标:release()
    time.sleep(1)
    ActionChains(driver).release().perform()
    time.sleep(1)
    
    
  9. achieve effect
    Insert image description here

Guess you like

Origin blog.csdn.net/sallyyellow/article/details/130206251