我使用 pyautogui 编写了一个脚本,该脚本应该启动一个程序(IDE),然后开始使用它。 这是到目前为止的脚本:
#! python3
# mouseNow.py - Displays the mouse cursor's current position.
import pyautogui, sys, subprocess
from time import sleep
x,y = 1100,550
subprocess.call([r'C:\...exe', arg1, arg2])
pyautogui.click(x,y)
sleep(5) # 2 sec should suffice but this is for safety
pyautogui.typewrite(my_string)
pyautogui.press('enter')
这很好用,但我想要便携。 x,y 值是由启动程序后程序提示符在屏幕上出现的位置决定的,但我认为这是不可移植的。有没有办法让鼠标指向提示而不给出 const 参数?类似 move_mouse_to_window_of_this_process_after_starting_it()
另外,我使用 sleep()
所以我会在窗口出现后将数据写入窗口,但我想这不是一个好方法(有些 PC 会运行我猜这要慢得多),那么有没有办法知道提示何时出现,然后执行pyautogui.typewrite(my_string)
?
编辑:我找到了 move_mouse_to_window_of_this_process_after_starting_it()
的简单解决方案
:
>>> pyautogui.hotkey('alt', 'tab')
最佳答案
如果您需要可移植且可靠的解决方案,您必须找到一个支持辅助功能技术的库,以通过文本访问 GUI 元素。基本技术有:
- Win32 API、MS UI 自动化 (Windows)
- AT-SPI(Linux)
- Apple 辅助功能 API (MacOS)
有几个开源 GUI 自动化库支持其中一些技术(通常是 1 或 2 种)。 Python解决方案:
- pywinauto在 Windows 上(Win32 API 和 MS UIA,请参阅 Getting Started Guide)
- pyatspi2在 Linux 上
- pyatom在 MacOS 上
还有a thread on StackOverflow关于硬 sleep 与灵活等待。
享受吧! :)
关于python - 使用 pyautogui 自动化鼠标和键盘的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42412157/