博客园模拟登录(常见滑块验证码破解)

import time

import PIL
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.by import By #找[某个页面元素] --定义[定位器]的主要东西
from selenium.webdriver.support.wait import WebDriverWait #显示等待对象
from selenium.webdriver.support import expected_conditions as EC #  [某个条件为止]
from selenium.webdriver.common.action_chains import ActionChains

def is_similar(x, y, image1, image2):
    pixel1=image1.getpixel((x,y))
    pixel2=image2.getpixel((x,y))
    if abs(pixel1[0]-pixel2[0])>=30 and abs(pixel1[1]-pixel2[1])>=30 and abs(pixel1[2]-pixel2[2])>=30:
        return False
    return True
#计算图片差异
def calculate_distance(image1, image2):
    height=image1.height
    width=image1.width
    for i in range(width):
        for j in range(height):
            if not is_similar(i,j,image1,image2):
                return (i,j)
    return -1
#获取缺口的偏差
def get_distance(driver):
    # 切入iframe
    driver.switch_to.frame(1)
    #全局截屏
    driver.save_screenshot("total_1.png")
    #找到抠图的参数
    yz_element=driver.find_element_by_xpath('/html/body/div[5]/div[2]/div[6]/div/div[1]/div[1]/div/a/div[1]')
    size=yz_element.size
    location=yz_element.location
    print(size)
    print(location)
    params_crop=(location['x']+65,location['y']+5,location['x']+size['width']-5,location['y']+size['height']-5)
    #抠图---有缺口局部图片image1
    image_t1=Image.open('total_1.png')
    image_part1=image_t1.crop(params_crop)
    image_part1.save()
    #更改js,出现原图
    driver.execute_script('document.getElementsByClassName("geetest_canvas_fullbg")[0].style="display: block"')
    driver.save_screenshot("total_2.png")
    image_t2 = Image.open('total_2.png')
    image_part2 = image_t2.crop(params_crop)
    image_part2.save()
    #计算二个图片之间的差距
    x,y=calculate_distance(image_part1,image_part2)
    return x+60
#控制滑块速度
def get_tracks(distance):
    #物理计算公式 s=v0*t+0.5*a*t*t
    #速度
    v=0
    #时间间隔
    t=0.3
    tracks=[]
    #当前距离
    current=0
    while current<distance:
        v0=v
        if current<distance*0.6:
            a=10
        else:
            a=-6
        s=v0*t+0.5*a*t*t
        tracks.append(round(s))
        current+=s
        v=v0+a*t
    return tracks
#滑块破解
def slider_code(driver):
    #找到滑块
    # slider=driver.find_element_by_xpath('/html/body/div[5]/div[2]/div[6]/div/div[1]/div[2]/div[2]')
    while True:
        #使用鼠标操作点住滑块并且使其悬浮
        # ActionChains(driver).click_and_hold(on_element=slider).perform()
        #计算移动的距离
        ActionChains(driver).move_by_offset(xoffset=0,yoffset=0).perform()
        distance=get_distance(driver)
        #移动的过程使用先加后减
        tracks=get_tracks(distance)
        for s in tracks:
            ActionChains(driver).move_by_offset(xoffset=s, yoffset=0).perform()
        # #移动到目标位置之后释放鼠标
        ActionChains(driver).release().perform()
        if '首页' in driver.page_source:
            break
        #点击刷新
        driver.find_element_by_xpath('/html/body/div[5]/div[2]/div[6]/div/div[2]/div/a[2]').click()
        time.sleep(1)
    return True
def main():
    #1.博客园登录页面
    driver.get('https://account.cnblogs.com/signin?returnUrl=https:%2F%2Fwww.cnblogs.com%2F')
    #2.点击登录
    wait.until(EC.presence_of_element_located((By.ID,'mat-input-0'))).send_keys('你的用户名')
    wait.until(EC.presence_of_element_located((By.ID,'mat-input-1'))).send_keys('你的密码')
    wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/app-root/div/mat-sidenav-container/mat-sidenav-content/div/div/app-sign-in/app-content-container/mat-card/div/form/div/button'))).click()
    time.sleep(3)
    if '首页' not in driver.page_source:
        #解决滑块问题
        if slider_code(driver):
            print("登录成功!!")
    else:
        print("登录成功!!")


if __name__ == '__main__':
    driver=webdriver.Chrome()
    wait=WebDriverWait(driver,20)
    driver.maximize_window()
    main()
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值