selenium破解某东滑块解锁

有参考其他的文章,自己也写一个。里面有注释。

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from time import sleep
import base64
from PIL import Image
from io import BytesIO
import cv2
import numpy as np
from PIL import Image as Im
import time
class demo3:
    def __init__(self):
        self.url = 'https://passport.jd.com/new/login.aspx'
        self.driver = webdriver.Chrome()
        self.wait = WebDriverWait(self.driver, 20)  # 设置等待时间20秒
        self.username = "账号"
        self.password = "密码"
        self.pianyi_x=0#x轴默认使用的偏移量
        self.pianyi_y = 0#y轴默认使用的偏移量
    def agreement_inputPhone(self):
        self.driver.get(self.url)
        self.driver.maximize_window()
        sleep(1)
        # 点击账户登录
        self.driver.find_elements_by_xpath('//div[@class="login-tab login-tab-r"]//a')[0].click()
        sleep(3)
        self.inputuser = self.wait.until(EC.presence_of_element_located((By.ID, "loginname")))
        self.inputuser.send_keys(self.username)
        self.inputpwd = self.wait.until(EC.presence_of_element_located((By.ID, "nloginpwd")))
        self.inputpwd.send_keys(self.password)
        sleep(1)
        # 点击登录按钮,出现验证码图片
        login_button = self.driver.find_elements_by_xpath('//div[@class="login-btn"]/a[@id="loginsubmit"]')[0]
        login_button.click()
        sleep(3)
    def decode_image(self):
        bigimg=self.driver.find_element_by_xpath('//div[@class="JDJRV-bigimg"]/img').get_attribute('src')
        image1_bytes = base64.urlsafe_b64decode(bigimg.split(',')[-1])
        f = open('big.png', 'wb')  # 若是'wb'就表示写二进制文件
        f.write(image1_bytes)
        f.close()
        smallimg = self.driver.find_element_by_xpath('//div[@class="JDJRV-smallimg"]/img').get_attribute('src')
        image2_bytes = base64.urlsafe_b64decode(smallimg.split(',')[-1])
        f2 = open('small.png', 'wb')  # 若是'wb'就表示写二进制文件
        f2.write(image2_bytes)
        f2.close()
    def find_pic(self,bigPath="big.png",smallPath="small.png"):
        otemp =bigPath
        oblk = smallPath
        #这个语句的意思就是说要读取图片,以单色的读取,要是参数为1(默认)的话,意思原彩读取
        target = cv2.imread(otemp, 0)
        template = cv2.imread(oblk, 0)
        #这个语句的意思就是说要读取一下这个大图的长高
        w, h = target.shape[::-1]
        ws,hs=template.shape[::-1]

        targ = 'targ.jpg'
        temp = 'temp.jpg'
        #把刚才的单色的通道的东西写出
        cv2.imwrite(temp, template)
        cv2.imwrite(targ, target)
        #读取灰度图
        target = cv2.imread(targ)
        target = cv2.cvtColor(target, cv2.COLOR_BGR2GRAY)
        #相当于对于颜色进行了取反亮色变成了黑色
        target = abs(255 - target)#取绝对值
        cv2.imwrite(targ, target)
        target = cv2.imread(targ)
        template = cv2.imread(temp)

        #同过归一化匹配方法查看后者在前者的位置,模糊匹配
        result = cv2.matchTemplate(target, template, cv2.TM_CCOEFF_NORMED)
        # 图片灰度最小的值,它的位置    图片灰度最大的值,他的位置
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
        #print(min_val, max_val, min_loc, max_loc)
        y,x = np.unravel_index(result.argmax(), result.shape)

        # 缺口位置
        print(max_loc,x,y)
        #print((x, y, x +ws, y + hs))
        
        # 调用PIL Image 做测试
        image = Im.open(otemp)

        xy = (x , y , x +ws, y +hs)
        # 切割
        imagecrop = image.crop(xy)
        # 保存切割的缺口
        imagecrop.save("new_image.png")
        self.pianyi_x=x
        self.pianyi_y=y
        bigc=cv2.imread('big.png')
        cv2.circle(bigc,(x,y),20,(0,0,255),0)
    def move_drive(self):
        huakuai=self.driver.find_element_by_xpath('//div[@class="JDJRV-slide-bg "]/div[@class="JDJRV-slide-inner JDJRV-slide-btn"]')
        tracks=self.get_track(self.pianyi_x)
        print('和为'+str(sum(tracks)))
        ActionChains(self.driver).click_and_hold(huakuai).perform()
        for x in tracks:
            ActionChains(self.driver).move_by_offset(xoffset=x, yoffset=0).perform()
        time.sleep(0.5)
        ActionChains(self.driver).release().perform()
        time.sleep(3)
    def get_track(self,distance):  # distance为传入的总距离
        # 移动轨迹
        track = []
        # 当前位移
        current = 0
        # 意思到哪个地方减速
        mid = distance * 9 / 10
        # 计算间隔
        t = 0.2
        # 初速度
        v = 1
        while current < distance:
            if current < mid:
                # 加速度为2
                a = 10
            else:
                # 加速度为-2
                a = -3
            v0 = v
            # 当前速度
            v = v0 + a * t
            # 移动距离
            move = v0 * t + 1 / 2 * a * t * t
            # 当前位移
            current += move
            # 加入轨迹
            track.append(round(move))
        track[-1]=track[-1]-sum(track)+self.pianyi_x
        print(track)
        return track
if __name__ == '__main__':
       demo=demo3()
       demo.agreement_inputPhone()
       while demo.url==demo.driver.current_url:
               demo.decode_image()
               demo.find_pic()
               demo.move_drive()




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值