pyautogui 等待元素出现的方法

pyautoguikit.py

import time
import pyautogui

# 等待元素出现
def waitUntilShow(imgPath, timeout=20):
    
    start_time = time.time()
    while time.time() - start_time < timeout:
        try:
            # 尝试在屏幕上定位图片
            position = pyautogui.locateOnScreen(image=imgPath,grayscale=False,confidence=0.9)
            if position:
                # 如果找到图片,退出循环
                print(f"Image postion: {position.left}, {position.top}")
                return pyautogui.center(position)
                break
            else:
                # 如果没有找到图片,继续循环
                print("Image not found ! waiting ...")
                time.sleep(1)  # 等待1秒
        except pyautogui.ImageNotFoundException:
            print("Image not found : " + str(time.time() - start_time))
            time.sleep(1)  # 等待1秒
    else:
        # 如果超出了超时时间,则元素未找到
        print("Image not found ! time out...")

# 点击元素    
def click(imgPath):
    pyautogui.click(pyautogui.center(pyautogui.locateOnScreen(image=imgPath,grayscale=False,confidence=0.9)))

# 点击元素直至元素出现
def clickUntilShow(imgPath,timeout):
    position = waitUntilShow(imgPath,timeout)
    pyautogui.click(position)
    return position

相关推荐

  1. pyautogui 等待元素出现方法

    2024-06-12 08:50:06       10 阅读
  2. html隐藏元素方法

    2024-06-12 08:50:06       30 阅读
  3. jQuery改变元素方法

    2024-06-12 08:50:06       20 阅读
  4. CSS隐藏元素方法

    2024-06-12 08:50:06       8 阅读
  5. Linux 内核中处理阻塞访问方法等待队列

    2024-06-12 08:50:06       24 阅读
  6. BUG:PyAutoGUI pyautogui.ImageNotFoundException

    2024-06-12 08:50:06       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-12 08:50:06       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-12 08:50:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-12 08:50:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-12 08:50:06       18 阅读

热门阅读

  1. app-ios 内嵌h5的缓存问题

    2024-06-12 08:50:06       5 阅读
  2. 简单聊聊Vue

    2024-06-12 08:50:06       9 阅读
  3. 微信小程序·审核

    2024-06-12 08:50:06       9 阅读
  4. Hive的存储格式和压缩算法的特点和选择

    2024-06-12 08:50:06       7 阅读
  5. React和Vue有什么区别

    2024-06-12 08:50:06       7 阅读
  6. ubuntu22.04禁止自动休眠的几种方式

    2024-06-12 08:50:06       8 阅读
  7. 算法训练营day53

    2024-06-12 08:50:06       7 阅读
  8. 代码随想录算法训练营day44

    2024-06-12 08:50:06       8 阅读
  9. 【环境搭建】3.阿里云ECS服务器 安装Redis

    2024-06-12 08:50:06       7 阅读