Python implements sliding verification

When doing automated testing, you need to automatically log in to the QQ mailbox. What you find on the Internet is based on selenium: refer to the link , but the code does not achieve satisfactory results when running locally: the sliding is unsuccessful !
Moreover, each webpage to be unlocked by sliding is different, the encoding and format are different, and the reuse rate is too low! for example:

button = browser.find_element_by_id('tcaptcha_drag_button')
x, y = button.location.get('x'), button.location.get('y')
print("location:",x,y)

This code is to get the coordinates of the sliding unlock button, but it returns (33,193), not the coordinates that should be obtained according to the desktop pixels (1920,1080).
Insert picture description here
Looking at the source code of the webpage, it is found that this verification box: width: 300px, height: 230px, so the coordinates are obtained according to this iframe frame, so there will be problems when dragging the button and sliding in the back, (for example, if you drag a little bit, an error will be reported: Exceeding the (300,230) boundary error), and always can't drag to the right, it won't work for a long time (partly because my front-end foundation is not strong)
There are some other problems I will not talk about, anyway, it is an explosion of mentality: I don't want to use selenium Engaged in sliding!
I changed the method to locate the image of the button, and then implement sliding verification: For specific image positioning, please refer to: ac.find_template to identify the image and locate it

Just upload the code:

#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import paramiko
import pyautogui
import os
from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from Auto_Test.base_action import match

def login_in():
    browser = webdriver.Firefox()
    browser.maximize_window()
    browser.get("https://mail.qq.com/")
    browser.switch_to.frame("login_frame")
    browser.find_element_by_class_name("inputstyle").clear()
    browser.find_element_by_class_name("inputstyle").send_keys("3540830376")
    browser.find_element_by_class_name("inputstyle.password").clear()
    browser.find_element_by_class_name("inputstyle.password").send_keys("LUO808089lin")
    browser.find_element_by_id("login_button").click()
    browser.find_element_by_class_name("login_button").click()
    time.sleep(2)
    browser.switch_to.frame("tcaptcha_iframe")

    time.sleep(3)
    # 等待图片加载出来
    WebDriverWait(browser, 5, 0.5).until(
        EC.presence_of_element_located((By.ID, "tcaptcha_drag_button")))
    # button = browser.find_element_by_id('tcaptcha_drag_button')
    # x, y = button.location.get('x'), button.location.get('y')
    # print("location:",x,y)
    lx,ly=match("QQmail-slick")
    lz=[x for x in range(120,200,3)]
    for i in lz:
        pyautogui.moveTo(lx,ly)
        pyautogui.dragRel(i,0,duration=2,button='left')
        time.sleep(1)
        pyautogui.moveRel(-i,0)

        try:
            alert = browser.find_element_by_id('guideText').text
        except Exception as e:
            print('get alert error: %s' % e)
            alert = ''
        if alert:
            print(u'滑块位移需要调整: %s' % alert)
            sleep(3)
        else:
            print('滑块验证通过')
            browser.switch_to.parent_frame()  # 验证成功后跳回最外层页面
            break

The functions in from Auto_Test.base_action import match are as follows:

def match(target, show=True):
    target_path = target.split("-")[0]
    target_name = target.split("-")[1]
    # 防止截全屏的页面没反应过来
    time.sleep(1)
    ttime_s = time.time()

    global root_dir
    appname = root_dir + '/APP_Action_Icons/'+target_path+"/"+target_name+'.png'
    appname_2 = root_dir + '/APP_Action_Icons/'+target_path+"/"+target_name+'_2.png'
    appname_3 = root_dir + '/APP_Action_Icons/'+target_path+"/"+target_name+'_3.png'
    print(appname)
    print("root_dir =", root_dir)

    ##根据桌面路径进行截屏
    hwnd = win32gui.FindWindow(None, root_dir)
    app = QApplication(sys.argv)
    screen = QApplication.primaryScreen()
    img = screen.grabWindow(hwnd).toImage()

    ##保存截屏
    root_desk = root_dir + '/Screen_Shots/Test_Screen_shots/desktop.jpg'
    img.save(root_desk)
    ##等待图片保存
    time.sleep(0.5)
    # 支持图片名为中英文名,也支持路径中英文
    imsrc = cv2.imdecode(np.fromfile(root_desk, dtype=np.uint8), -1)
    imobj = cv2.imdecode(np.fromfile(appname, dtype=np.uint8), -1)

    ##根踞名称匹配截图,只支持图片为英文名
    # imsrc = ac.imread(root_desk)
    # imobj = ac.imread(appname)

    # 匹配图标位置
    pos = ac.find_template(imsrc, imobj, 0.5,bgremove=True)
    if pos == None and os.path.exists(appname_2):
        print("第一张图标没匹配到,现匹配第二张:", end="")
        print(appname_2)
        imobj_2 = cv2.imdecode(np.fromfile(appname_2, dtype=np.uint8), -1)
        # imobj_2 = ac.imread(appname_2)
        pos = ac.find_template(imsrc, imobj_2, 0.5,bgremove=True)
    if pos == None and os.path.exists(appname_3):
        print("第二张图标没匹配到,现匹配第三张:", end="")
        print(appname_3)
        imobj_3 = cv2.imdecode(np.fromfile(appname_3, dtype=np.uint8), -1)
        # imobj_3 = ac.imread(appname_3)
        pos = ac.find_template(imsrc, imobj_3, 0.5,bgremove=True)

    # 如果第三张还未匹配到,用另一种方法重新截图
    if pos == None:
        print("2秒后重新截全屏...")
        time.sleep(2)

        ##保存截屏
        root_desk = root_dir + '/Screen_Shots/Test_Screen_shots/desktop_2.jpg'
        img = ImageGrab.grab()
        img.save(root_desk)
        ##等待图片保存
        time.sleep(0.5)
        # 支持图片名为中英文名,也支持路径中英文
        imsrc = cv2.imdecode(np.fromfile(root_desk, dtype=np.uint8), -1)
        imobj = cv2.imdecode(np.fromfile(appname, dtype=np.uint8), -1)


        # 匹配图标位置
        pos = ac.find_template(imsrc, imobj, 0.5,bgremove=True)
        if pos == None and os.path.exists(appname_2):
            print("第一张图标没匹配到,现匹配第二张:", end="")
            print(appname_2)
            imobj_2 = cv2.imdecode(np.fromfile(appname_2, dtype=np.uint8), -1)
            pos = ac.find_template(imsrc, imobj_2, 0.5,bgremove=True)
        if pos == None and os.path.exists(appname_3):
            print("第二张图标没匹配到,现匹配第三张:", end="")
            print(appname_3)
            imobj_3 = cv2.imdecode(np.fromfile(appname_3, dtype=np.uint8), -1)
            pos = ac.find_template(imsrc, imobj_3, 0.5,bgremove=True)

    if pos == None:
        print("最终没能匹配到:" + target)
    else:
        ttime_e = time.time()
        __time = ttime_e - ttime_s

        if show == True:
            try:
                show_and_save(root_desk, target, pos, __time)
            except Exception as e:
                print("保存匹配结果show_and_save这里出错,错误原因为{}".format(e))
        print(pos)
        point = pos['result']
        pyautogui.moveTo(point)
        print("匹配成功:{}".format(target))
        time.sleep(0.5)
        print(pyautogui.position())
        pyautogui.click(clicks=2)
        return point

The pro-test is effective, if you have any questions, you can contact me, and you will reply as soon as you see it!
Communicate with each other, learn from each other, and make progress together!

Guess you like

Origin blog.csdn.net/liulanba/article/details/115296586