Python 自动化图像点击

问题描述 投票:0回答:1

我正在尝试制作一个机器人,当图像/按钮出现在网页上时单击它们。我不能使用 selenium,因为我需要安装扩展并且不能是新的浏览器。

我的第一部分可以工作,但循环在重新启动到第一次单击操作后停止,并表示找不到图像。我只需要机器人继续寻找图像并单击,即使单击或未找到图像,也可以继续循环。

import time
import pyautogui
import win32api
import win32con

time.sleep(5)


def click():
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
    time.sleep(0.1)

def click2():
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
    time.sleep(.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)

def try_click_actions():
    while True:
        if click_action_1():
            print("Click action 1 succeeded!")
        elif click_action_2():
            print("Click action 2 succeeded!")
        elif click_action_3():
            print("Click action 3 succeeded!")
        else:
            try_click_actions()

def click_action_1():
    start = pyautogui.locateCenterOnScreen('images/image1.png', region=(0, 0, 1920, 1080), confidence=.8)
    if start is not None:
        pyautogui.moveTo(start)
        click()
        time.sleep(5)

    

def click_action_2():
    start2 = pyautogui.locateCenterOnScreen('images/image2.png', region=(0, 0, 1920, 1080), confidence=.8)
    if start2 is not None:
        pyautogui.moveTo(start2)
        click2()
        time.sleep(18.5)
    

def click_action_3():
    start3 = pyautogui.locateCenterOnScreen('images/image3.png', region=(0, 0, 1920, 1080), confidence=.8)
    if start3 is not None:
        pyautogui.moveTo(start3)
        click()
        time.sleep(10)
    

try_click_actions()

python automation bots
1个回答
0
投票

你可以试试这个,你可以在代码中找到注释说明:

import time
import pyautogui
import win32api
import win32con

time.sleep(5)

def click():
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
    time.sleep(0.1)

def click_action(image, delay_after_click):
    location = pyautogui.locateCenterOnScreen(image, region=(0, 0, 1920, 1080), confidence=0.8)
    if location is not None:
        pyautogui.moveTo(location)
        click()
        print(f"Clicked on {image}")
        time.sleep(delay_after_click)
        return True
    return False

def try_click_actions():
    while True:
        found_image = False
        
        # Attempt each click action in sequence. If one is found and clicked, set found_image to True.
        if click_action('images/image1.png', 5):
            found_image = True
        elif click_action('images/image2.png', 18.5):
            found_image = True
        elif click_action('images/image3.png', 10):
            found_image = True
        
        # If no images were found and clicked, sleep for a short duration before trying again.
        if not found_image:
            time.sleep(1)  # Adjust this sleep time as needed.

try_click_actions()

© www.soinside.com 2019 - 2024. All rights reserved.