基于selenium完成滑块验证

import time
from selenium import webdriver
from selenium.webdriver.common.by import By  
import cv2
from selenium.webdriver.common.action_chains import ActionChains
from urllib import request


def get_distance():
    background = cv2.imread('background_src.png', 0)
    gap = cv2.imread('gpg_src.png', 0)
    res = cv2.matchTemplate(background, gap, cv2.TM_CCOEFF_NORMED)
    value = cv2.minMaxLoc(res)[2][0]
    print(value)
    # 比例转换 下载的图片与实际图片有一定的比例
    return value * 278 / 360


def main():
    chrome = webdriver.Chrome()
    chrome.implicitly_wait(5)
    chrome.get('https://passport.jd.com/new/login.aspx')
    login = chrome.find_element(By.CLASS_NAME, 'login-tab-r')
    login.click()
    time.sleep(5)
    username = chrome.find_element(By.ID, 'loginname')
    username.send_keys('aaaaaaa')
    time.sleep(1)
    password = chrome.find_element(By.ID, 'nloginpwd')
    password.send_keys('asdfasdf')
    button = chrome.find_element(By.ID, 'loginsubmit')
    button.click()
    time.sleep(3)
    background_src = chrome.find_element(By.XPATH, '//div[@class="JDJRV-bigimg"]/img').get_attribute("src")
    gap_src = chrome.find_element(By.XPATH, '//div[@class="JDJRV-smallimg"]/img').get_attribute("src")
    request.urlretrieve(background_src, 'background_src.png')
    request.urlretrieve(gap_src, 'gpg_src.png')
    # 计算滑块距离
    distance = int(get_distance())
    # 滑动滑块
    element = chrome.find_element(By.CLASS_NAME, 'JDJRV-slide-btn')
    ActionChains(chrome).click_and_hold(on_element=element).perform()  # 模拟鼠标按住不放
    ActionChains(chrome).move_by_offset(xoffset=distance, yoffset=0).perform()
    ActionChains(chrome).release(on_element=element).perform()
    time.sleep(5)


if __name__ == "__main__":
    main()

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值