Python实训day7

from selenium import webdriver
from selenium.webdriver import ActionChains
import time
from PIL import Image
import random

def cut_image(driver):
    #获取整个页面图片,图片名字为:'snap.png'
    driver.save_screenshot('snap.png')
    # 获取滑动小图片
    image = driver.find_element_by_class_name('geetest_canvas_img')
    print(image.location)
    print(image.size)

    # 获取小图片的左上右下的位置
    left = image.location['x']
    top = image.location['y']
    right = left + image.size['width']
    button = top + image.size['height']
    print(left,top,right,button)
    #调用open方法打开全屏图片并赋值给image_obj对象
    image_obj = Image.open('snap.png')

    # 通过image_obj对象对小图片进行截取
    # 传个元祖,按顺序(左,上,右,下)
    img = image_obj.crop((left,top,right,button))
    #img.show()
    return img



def get_image1(driver):
    time.sleep(2)
    # 修改document文档树,把完整的图片display属性修改为block
    js_code = '''
        var x = document.getElementsByClassName('geetest_canvas_fullbg')[0].style.display = "block";
    '''
    # 执行js代码
    driver.execute_script(js_code)

    image = cut_image(driver)

    return image


def get_image2(driver):
    time.sleep(2)
    # 修改document文档树,把完整的图片display属性修改为block
    js_code = '''
        var x = document.getElementsByClassName('geetest_canvas_fullbg')[0].style.display = "none";
    '''
    # 执行js代码
    driver.execute_script(js_code)

    image = cut_image(driver)

    return image


def get_distance(image1,image2):
    # 小滑块右侧位置
    start = 60
    # 像素差
    num = 60

    for x in range(start,image1.size[0]):
        for y in range(image1.size[1]):
            # 获取imag1完整图片的每一个坐标的像素点
            rgb1 = image1.load()[x,y]
            # 获取imag2缺口图片的每一个坐标的像素点
            rgb2 = image2.load()[x,y]
            print(rgb1,rgb2)

            # abs获取绝对值,像素点比较的值
            r= abs(rgb1[0] - rgb2[0])
            g= abs(rgb1[1] - rgb2[1])
            b= abs(rgb1[2] - rgb2[2])

            # 如果条件成立,则找到缺口位置
            if not(r<num and g<num and b<num):
                # 有误差 -7
                return x-7


def get_strck_move(distance):
    distance += 20
    # 初速度
    v0 = 0
    # 时间
    t = 0.2
    # 位置
    s=0
    # 滑动轨迹列表
    move_list = []

    # 中间值,作为加减速度的位置
    mid = distance / 5 * 3

    # 加减速度列表
    v_list = [1,2,3,4]


    while s< distance:
        if s< mid:
            a = v_list[random.randint(0,len(v_list)-1)]
        else:
            a = -v_list[random.randint(0, len(v_list) - 1)]

        # 获取初始速度
        v = v0

        # 路程公式:
        s1 = v * t + 0.5 * a * (t ** 2)
        s1 = round(s1)  # 取整

        # 加速公式:
        # v = v0 + a * t
        m_v = v + a * t

        # 把当前加/减速度赋值给初始速度,以便下一次计算
        v0 = m_v

        # 把位移添加到滑动列表中
        move_list.append(s1)

        # 修改滑动初始距离
        s += s1

    # 后退列表,自定义后退滑动轨迹
    back_list = [-1,-1,-2,-3,-2,-1,-1,-2,-3,-2,-1,-1]

    return {'move_list': move_list, 'back_list': back_list}


def main():
    driver = webdriver.Chrome()
    driver.implicitly_wait(10)
    driver.get('https://account.cnblogs.com/signin?returnUrl=https%3A%2F%2Fwww.cnblogs.com%2F')

    user_input = driver.find_element_by_id('LoginName')
    user_input.send_keys('lyj68')
    time.sleep(0.2)

    pwd_input = driver.find_element_by_id('Password')
    pwd_input.send_keys('1998223689lyj')
    time.sleep(2)

    login_submit = driver.find_element_by_id('submitBtn')
    login_submit.click()

    # 获取完整图片
    image1 = get_image1(driver)

    # 获取有缺口图片
    image2 = get_image2(driver)
    # 比较两张图片,获取滑动距离
    distance = get_distance(image1,image2)
    print(distance)

    #模拟人的滑动轨迹
    move_dict = get_strck_move(distance)
    move_list = move_dict['move_list']
    back_list = move_dict['back_list']


    # 滑动开始
    move_tag = driver.find_element_by_class_name('geetest_slider_button')
    ActionChains(driver).click_and_hold(move_tag).perform()

    for move in move_list:
        ActionChains(driver).move_by_offset(xoffset=move,yoffset=0).perform()
        time.sleep(0.1)

    for back in back_list:
        ActionChains(driver).move_by_offset(xoffset=back,yoffset=0).perform()
        time.sleep(0.1)

    # 制作微妙晃动
    ActionChains(driver).move_by_offset(xoffset=3,yoffset=0).perform()
    ActionChains(driver).move_by_offset(xoffset=-3,yoffset=0).perform()

    time.sleep(0.1)
    ActionChains(driver).release().perform()
    time.sleep(10)

if __name__ == '__main__':
    main()
上一篇:opencv中Mat究竟是什么?


下一篇:vue-router多个组件模板放入同一个页面中