Prozacs 发表于 2021-8-27 11:47

顶象无感验证码demo

import cv2
import numpy as np
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains #引入ActionChains鼠标操作类
from selenium.webdriver.chrome.options import Options
import time
import requests
def get_track(distance):      # distance为传入的总距离
    track=[]
    current=0
    mid=distance*4/5
    t=0.2
    v=10
    while current<distance:
      if current<mid:
            a=2
      else:
            a=-3
      v0=v
      v=v0+a*t
      move=v0*t+1/2*a*t*t
      current+=move
      track.append(round(move))
    return track

def move_to_gap(driver,slider,tracks):   # slider是要移动的滑块,tracks是要传入的移动轨迹
    ActionChains(driver).click_and_hold(slider).perform()
    for x in tracks:
      ActionChains(driver).move_by_offset(xoffset=x,yoffset=0).perform()
    time.sleep(0.5)
    ActionChains(driver).release().perform()

def show(name):
    cv2.imshow('Show', name)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def main():
    otemp = 'dx2.png'
    oblk = 'dxbgg.png'
    target = cv2.imread(otemp, 0)
    #target = cv2.resize(targetsrc, (0, 0), fx=0.75, fy=0.75, interpolation=cv2.INTER_NEAREST)
    #show(target)
    template = cv2.imread(oblk, 0)
    show(template)
    w, h = target.shape[::-1]
    temp = 'temp.jpg'
    targ = 'targ.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)
    x, y = np.unravel_index(result.argmax(), result.shape)
    # 展示圈出来的区域
    cv2.rectangle(template, (y, x), (y + w, x + h), (7, 300, 150), 2)
    show(template)
    return y


chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9221")
browser = webdriver.Chrome(r'D:\pythongc\pythongc\chromedriver.exe', options=chrome_options)
browser.get('http://cdn.dingxiang-inc.com/ctu-group/captcha-ui/demo/')

time.sleep(3) #3m
ele = browser.find_element_by_css_selector('#dx_captcha_basic_bg_1 > canvas')
#browser.execute_script('') #隐藏再截图
browser.execute_script('''
$("#dx_captcha_basic_sub-slider_1").hide();
$("#dx_captcha_basic_init-inform_1").hide();
$("#dx_captcha_basic_bar_1").hide();
$("#demo-embed").width("400px").height("200px");
$("#dx_captcha_basic_pic_1").height("200px");
$("#dx_captcha_basic_bg_1").height("200px");
$("#dx_captcha_basic_bg_1").children().css('width','400px').css('height','200px');
''') #隐藏再截图
time.sleep(1) #3m
ele.screenshot('dxbgg.png')
browser.execute_script('''
$("#dx_captcha_basic_sub-slider_1").show();
$("#dx_captcha_basic_init-inform_1").show();
$("#dx_captcha_basic_bar_1").show();
$("#demo-embed").width("300px").height("150px");
$("#dx_captcha_basic_pic_1").height("150px");
$("#dx_captcha_basic_bg_1").height("150px");
$("#dx_captcha_basic_bg_1").children().css('width','300px').css('height','150px');
''') #隐藏再截图




imgurl = browser.find_element_by_css_selector('#dx_captcha_basic_sub-slider_1 > img').get_attribute('src')
response = requests.get(imgurl).content
with open( 'dx2.png','wb' ) as f:
    f.write(response)

result =main()
print(result)
pos = int((result-10) *0.75)
print(pos)
huakuai =browser.find_element_by_css_selector('#dx_captcha_basic_slider-img-normal_1')
# move_to_gap(browser,huakuai,get_track(pos))
ActionChains(browser).click_and_hold(huakuai).perform()
ActionChains(browser).move_by_offset(xoffset=pos,yoffset=0).perform()
#time.sleep(1)
#ActionChains(browser).release().perform()
#trs = pos / 5
#last = pos %5
#ActionChains(browser).move_by_offset(xoffset=trs,yoffset=0).perform()
#time.sleep(0.2)
#ActionChains(browser).move_by_offset(xoffset=trs,yoffset=0).perform()
#time.sleep(0.1)
#ActionChains(browser).move_by_offset(xoffset=trs,yoffset=0).perform()
#time.sleep(0.08)
#ActionChains(browser).move_by_offset(xoffset=trs,yoffset=0).perform()
#time.sleep(0.12)
#ActionChains(browser).move_by_offset(xoffset=trs,yoffset=0).perform()
#time.sleep(0.2)
#if last> 0:
#    ActionChains(browser).move_by_offset(xoffset=last,yoffset=0).perform()
time.sleep(0.5)
ActionChains(browser).release().perform()
message = browser.find_element_by_xpath('//*[@id="dx_captcha_basic_bar-success_1"]').text
print(message)

a603558762 发表于 2021-8-27 15:47

逆协议 啊

Prozacs 发表于 2021-8-27 17:33

a603558762 发表于 2021-8-27 15:47
逆协议 啊

{:1_905:}不能发啊
页: [1]
查看完整版本: 顶象无感验证码demo