Python 通过pyautogui进行自动化操作,截图定位

1、安装pyautogui

pip install pyautogui

2、利用pyautogui操作鼠标、通过截图定位

import pytest
import pyautogui
import os
import time
import allure
from PIL import Image
import cv2
import aircv as ac
import numpy as np


if __name__ == "__main__":
    # pytest.main(['-s', r'D:\aeas\shendu_-ui_zidonghua\tests\test_zaoshang\test_zaoshang.py'])
    # # 获取屏幕分辨率
    # print(pyautogui.size())
    # # # 获取鼠标的坐标位置
    # w, h = pyautogui.position()
    w, h = pyautogui.size()
    print("W: %s H: %s" % (w, h))
    # all = pyautogui.locateCenterOnScreen('allure.png',grayscale=False)
    # print(all, type(all))
    # print(tuple(all))
    # pyautogui.moveTo(all, duration=1)
    lock = pyautogui.locateCenterOnScreen('lock.png',grayscale=False)
    pyautogui.moveTo(lock, duration=0.5)
    pyautogui.leftClick()
    base_path = os.path.join(os.getcwd(), 'mima')
    time.sleep(0.5)
    for item in os.listdir(base_path):
        one_but = pyautogui.locateCenterOnScreen(os.path.join(base_path, item),grayscale=False)
        print(item, one_but)
        if one_but:
            pyautogui.moveTo(one_but, duration=0.5)

    # # 获取当前背景截图,用了获取坐标
    # img = pyautogui.screenshot(region=[0,0,w,h])
    # # print(dir(img))
    # # print(img.im)
    # # print(np.array(img))
    # pos = ac.find_template(np.array(img), ac.imread('lock.png'))
    # lock = pos.get('result')
    # pyautogui.moveTo(lock, duration=0.5)
    # pyautogui.leftClick()
    # time.sleep(0.5)
    # img = pyautogui.screenshot(region=[0,0,w,h])
    # base_path = os.path.join(os.getcwd(), 'mima')
    # for item in os.listdir(base_path):
    #     one_but = ac.find_template(np.array(img), ac.imread(os.path.join(base_path, item)), threshold=0.9)
    #     print(item, one_but)
    #     # 得到位置后,移动鼠标
    #     if one_but and one_but.get('result'):
    #         pyautogui.moveTo(one_but.get('result'), duration=0.3)

猜你喜欢

转载自blog.csdn.net/haeasringnar/article/details/115626899
今日推荐