[英]How can I click buttons in a windows GUI program using python?
I am writing a python script that automates running a program and performing different tasks within the program.我正在编写一个 python 脚本,它可以自动运行程序并在程序中执行不同的任务。 My main problem is figuring out how to click buttons and interact with the GUI of the program to be controlled.我的主要问题是弄清楚如何单击按钮并与要控制的程序的 GUI 进行交互。
I am currently using the pyautogui library and using pyautogui.click(X,Y) to advance through prompts and click on different menus and menu items.我目前正在使用 pyautogui 库并使用 pyautogui.click(X,Y) 来推进提示并单击不同的菜单和菜单项。 The problem with this approach is that I am relying on a separate script to inform me of the coordinates of interest in my environment by telling me the coordinates of where my cursor is hovering.这种方法的问题在于,我依靠单独的脚本通过告诉我 cursor 悬停位置的坐标来通知我环境中感兴趣的坐标。 This probably will not work on other machines and just seems like a one case solution.这可能不适用于其他机器,并且似乎只是一种解决方案。
My question is how can I automate using a program in windows (clicking around) without having to hard code the exact position of the items I need to click?我的问题是如何使用 windows 中的程序(点击)实现自动化,而不必对我需要点击的项目的确切 position 进行硬编码?
For example, If I need to click a "ok" box to accept some setting, how can I make Windows grab the program window, read through the options and click what I need without any prior knowledge of the position of the dialog box and where the "Ok" button is located?例如,如果我需要单击“确定”框接受某些设置,如何让 Windows 抓取程序 window,通读选项并单击我需要的内容,而无需事先了解对话框的 Z4757FE07FD492A8BE0EA6A760D 和 where 683D6 “确定”按钮在哪里?
Code:代码:
import pyautogui as gui
gui.click(x,y)
The way you can do this using pyautogui is with their locating methods.您可以使用 pyautogui 执行此操作的方法是使用它们的定位方法。 You will need a picture (for example of the OK box) and then you can have pyautogui find it on the screen and give you its coordinates.您将需要一张图片(例如 OK 框),然后您可以让 pyautogui 在屏幕上找到它并为您提供它的坐标。 Check out the official documentation on this .查看有关此的官方文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.