当无法通过元素定位时,可以通过截图定位。
mport os
import time
import cv2
from PIL import ImageGrab
from pymouse import PyMouse
from pykeyboard import PyKeyboard
from config import Config
class ImageMatch():
def __init__(self, target_image):
"""给一个目标图"""
self.target_image = target_image
self.mouse = PyMouse() # 鼠标
self.keyboard = PyKeyboard() # 键盘
self.__screenshot_full() # 调用一次截图
self.match_result = self.__match() # 调用一次匹配
def __screenshot_full(self):
"""截个全屏的图"""
self.full_screen_file_name = os.path.join(Config.root_path, f"image/full_screen_image/{int(time.time())}.png")
ImageGrab.grab().save(self.full_screen_file_name)
def __match(self) -> tuple:
"""匹配图并返回(最佳匹配度,中间坐标点,标记图)"""
big = cv2.imread(self.full_screen_file_