一、验证开发环境

from selenium import webdriver

driver=webdriver.Chrome()#chrome不要用绿色版

driver.get('https://www.baidu.com')

print(driver.title)

driver.quit

二、开发步骤

1.访问url地址,定位到登录密码位置。并点击账号密码。

2.密码清空,并重新输入。

3.再定位滑块,模拟轨迹图,范围在180

4.模拟人的行为

#!/usr/bin/python
# -*- coding: utf-8 -*-
#强制等待
from time import sleep
from selenium import webdriver
#鼠标事件
from selenium.webdriver import ActionChains
#显式等待
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

url = 'https://i.qq.com/'

def get_track(distance):
    """
    根据偏移量获取移动轨迹
    :param distance: 偏移量
    :return: 移动轨迹
    """
    # 移动轨迹
    track = []
    # 当前位移
    current = 0
    # 减速阈值
    mid = distance * 4 / 5
    # 计算间隔
    t = 1
    # 初速度
    # v = 0
    v = 0
    while current < distance:
        if current < mid:
            # 加速度为正2
            # a_b = 8
            a = 10
        else:
            # 加速度为负3
            a = -3
        # 初速度v0
        v0 = v
        # 当前速度v = v0 + at
        v = v0 + a * t
        # 移动距离x = v0t + 1/2 * a_b * t^2
        move = v0 * t + 1 / 2 * a * t * t
        # 当前位移
        current += move
        # 加入轨迹
        track.append(round(move))
    return track



def main():

    driver = webdriver.Chrome()
    driver.set_window_position(900,10)
    #窗口最大化
    driver.maximize_window()

    driver.get(url)
    driver.switch_to.frame('login_frame')
    driver.find_element_by_id('switcher_plogin').click()
    sleep(1)

    driver.find_element_by_id('u').clear()
    driver.find_element_by_id('u').send_keys('qq number')
    driver.find_element_by_id('p').clear()
    driver.find_element_by_id('p').send_keys('123')
    sleep(1)

    driver.find_element_by_id('login_button').click()

    sleep(5)

    try:
        iframe = driver.find_element_by_xpath('//iframe')
    except Exception as e:
        print(e)

    sleep(1)
    driver.switch_to.frame(iframe)
    #显式等待
    WebDriverWait(driver,5,0.5).until(EC.presence_of_element_located((By.ID,'tcaptcha_drag_button')))

    try:
        button = driver.find_element_by_id('tcaptcha_drag_button')
    except Exception as e:
        pass

    sleep(1)

    distance = 180
    times = 0

    while True:
        try:
            action = ActionChains(driver)
            #左键按住
            action.click_and_hold(button).perform()
            #清楚之前啊action
            action.reset_actions()
            #模拟轨迹方程

            track = get_track(distance)
            #[1,10,20,50,60]

            for i in track:
                #移动滑块
                action.move_by_offset(xoffset=i,yoffset=0).perform()
                #必加 清楚动作
                action.reset_actions()
            sleep(0.5)
            #释放鼠标
            action.release().perform()

            sleep(5)

            times += 1
            print('这个是滴几次{}'.format(times))

        except:
            print('登录成功')
            break

    print(driver.title)
    sleep(2)
    driver.quit()
    print('finish~~~')


    # sleep(5)
    # driver.quit()

if __name__ == '__main__':
    main()