简体   繁体   English

pywinauto - 如何识别 GUI 对象

[英]pywinauto - How to identify GUI objects

I am fairly new to Python and I am having trouble identifying GUI objects that I am hoping to control using pywinauto.我是 Python 的新手,我无法识别我希望使用 pywinauto 控制的 GUI 对象。

Following this example with the below code, I am able to open Notepad.exe and select "Help" from the Menu object.按照下面代码的示例,我可以从菜单 object 打开 Notepad.exe 和 select“帮助”。

from pywinauto.application import Application
# Run a target application
app = Application().start("notepad.exe")
# Select a menu item
app.UntitledNotepad.menu_select("Help->About Notepad")
# Click on a button
app.AboutNotepad.OK.click()
# Type a text string
app.UntitledNotepad.Edit.type_keys("pywinauto Works!", with_spaces = True)

This is pretty cool, but I want to apply this to a more practical example.这很酷,但我想将其应用到更实际的示例中。 What I am trying to do is open Excel using app = Application().start(r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE") and select "Blank Workbook" from the menu pane that pops up when you start Excel 2016 - thereby opening a new workbook.我想要做的是使用app = Application().start(r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE")打开 Excel 并从弹出的菜单窗格中打开 select“空白工作簿”当你开始时 Excel 2016 - 从而打开一个新的工作簿。

在此处输入图像描述

I've targeted the object using UISpy and identified that the name is "Blank workbook".我使用 UISpy 定位了 object 并确定名称为“空白工作簿”。 Using the above example code, what is the line of code I should execute that will select this object to open a new workbook?使用上面的示例代码,我应该执行的代码行是 select this object 打开一个新的工作簿? And more importantly, how do I figure that information out for myself?更重要的是,我如何自己找出这些信息?

在此处输入图像描述

I am using Python 3.6.1.我正在使用 Python 3.6.1。 In an unrelated question - I found it interesting that I'm able to open "notepad.exe" without the fully qualified name, but opening Excel requires app = Application().start(r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE") - I'm not sure why this is the case, but that's a question for another day...在一个不相关的问题中-我发现有趣的是我可以在没有完全限定名称的情况下打开“notepad.exe”,但是打开 Excel 需要app = Application().start(r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE") - 我不确定为什么会这样,但这是另一天的问题......

You can use external tools like SWAPY, pyinspect to identify the objects names.您可以使用 SWAPY、pyinspect 等外部工具来识别对象名称。 a full list can be found here:https://pywinauto.readthedocs.io/en/latest/getting_started.html完整列表可以在这里找到:https ://pywinauto.readthedocs.io/en/latest/getting_started.html

Regarding the python usage, you can trigger print_control_identifiers() function on each of pywinauto dialog objects.关于python的用法,您可以在每个pywinauto对话框对象上触发print_control_identifiers()函数。 the function shows all the dialog's objects: buttons/labels/checkboxes etc.. you can read about it via here: https://pywinauto.readthedocs.io/en/latest/code/code.html#controls-reference该函数显示了对话框的所有对象:按钮/标签/复选框等。你可以通过这里阅读它: https : //pywinauto.readthedocs.io/en/latest/code/code.html#controls-reference

So basically, try app.print_control_identifiers() check if the button's name is there and if it does just use getattr(app, X).click() while X is the button's name.所以基本上,尝试 app.print_control_identifiers() 检查按钮的名称是否在那里,如果它只是使用 getattr(app, X).click() 而 X 是按钮的名称。

  1. As I can see in your question, you already know about UISpy, (I prefer inspect.exe for Windows).正如我在您的问题中所看到的,您已经了解 UISpy(我更喜欢 Windows 的 inspect.exe)。 Such tools help find UI objects and their properties.此类工具有助于查找 UI 对象及其属性。 We can use properties like AutomationId, classname etc to access the UI objects.我们可以使用 AutomationId、classname 等属性来访问 UI 对象。

在此处输入图片说明

Here is the code snippet to open blank workbook in EXCEL.这是在 EXCEL 中打开空白工作簿的代码片段。

from pywinauto import Application
import time
app = Application(backend="uia")
excelApp = app.start(r"C:\\Program Files (x86)\\Microsoft Office\\Office15\\EXCEL.EXE")
time.sleep(5)
wind = excelApp.windows()
title = wind[0].get_properties()[u'texts']
handle = excelApp.window(title = title[0])
child = handle.child_window(auto_id="AIOStartDocument")
child.click_input()

Also, you should wait() method instead of sleep time, that is a better practice.此外,您应该使用 wait() 方法而不是睡眠时间,这是更好的做法。

  1. Another way is to get the object for the Excel mainwindow and then use the AccessKey (shortcut key) for the action.另一种方法是获取 Excel 主窗口的对象,然后使用 AccessKey(快捷键)进行操作。

If you manually open excel and press ALT key you will find out the shortcuts.如果您手动打开excel并按ALT键,您将找到快捷方式。 在此处输入图片说明

from pywinauto.application import Application
app = Application().start(cmd_line=u'"C:\\Program Files (x86)\\Microsoft Office\\Office15\\EXCEL.EXE" ')
xlmain = app.Excel
xlmain.wait('ready')
xlmain.type_keys("%L")

Use MS-Spy for object identification使用MS-Spy进行object识别

MS-SPY 对象间谍

https://github.com/munnasarfraz/SPY.git https://github.com/munnasarfraz/SPY.git

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM