python实现指定位置自动点击

以下代码通过截图来寻找点击的位置(截图可以是按钮,也可以是你想点击的其它部分)
通过image_path指定截图的路径

import pyautogui
import time

def click():
    # 获取当前屏幕分辨率(宽度和高度)
    screen_width, screen_height = pyautogui.size()
    print(f"屏幕分辨率:宽度 = {screen_width}, 高度 = {screen_height}")

    # 获取当前屏幕的截图
    screenshot = pyautogui.screenshot()

    # 获取截图的大小(宽度和高度)
    screenshot_width, screenshot_height = screenshot.size
    print(f"截图大小:宽度 = {screenshot_width}, 高度 = {screenshot_height}")

    # 计算截图大小与屏幕分辨率之间的比例
    width_ratio = screenshot_width / screen_width
    height_ratio = screenshot_height / screen_height
    print(f"宽度比例 = {width_ratio}, 高度比例 = {height_ratio}")

    # 找到对话框中的“确认”按钮并点击它
    while True:
        try:
            # 捕获屏幕截图并尝试找到“确认”按钮的图片
            image_path = '/Users/alpha/Desktop/click_ok/button.jpg'
            location = pyautogui.locateOnScreen(image_path, confidence=0.8)

            # 如果找到了“确认”按钮的位置,则点击它
            if location:
                center = pyautogui.center(location)
                x = center.x / width_ratio
                y = center.y / height_ratio
                pyautogui.click(x, y)
                print("click",x,y)
                #break

        except pyautogui.ImageNotFoundException:
                # 如果找不到图像,处理异常
                print(f"Image not found on screen: {image_path}")

if __name__ == "__main__":
    click()



在当今竞争激烈的软件开发市场中,选择一个可靠且经验丰富的开发团队至关重要。我们的团队专注于为……
2024-04-17

扫描关注程序区

有任何问题,请联系邮箱:support@progdomain.com

THE END
分享
二维码
打赏
海报
python实现指定位置自动点击
以下代码通过截图来寻找点击的位置(截图可以是按钮,也可以是你想点击的其它部分) 通过image_path指定截图的路径 import pyautogui import time def click(……
<<上一篇
下一篇>>