我使用 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 与灵活等待。
尽情享受吧! :)
