windows 桌面GUI自动化- 18.pywinauto 保存控件菜单树结构print_control_identifiers()
  iKXeBmqdRgYF 2023年11月02日 46 0

前言

.pywinauto 可以使用 print_control_identifiers() 方法打印控件菜单树结构,这对我们查找控件非常方便。

print_control_identifiers()

查看相关源码

def print_control_identifiers(self, depth=None, filename=None):
        """
        Prints the 'identifiers'

        Prints identifiers for the control and for its descendants to
        a depth of **depth** (the whole subtree if **None**).

        .. note:: The identifiers printed by this method have been made
               unique. So if you have 2 edit boxes, they won't both have "Edit"
               listed in their identifiers. In fact the first one can be
               referred to as "Edit", "Edit0", "Edit1" and the 2nd should be
               referred to as "Edit2".
        """
       
    print_ctrl_ids = print_control_identifiers
    dump_tree = print_control_identifiers

print_ctrl_ids 和 dump_tree 实现的功能与print_control_identifiers等价,都是调用的print_control_identifiers 方法。
用2个参数

  • depth 查找框架深度,默认全部查找
  • filename 保存本地文件名称

保存本地文件

把打印的内容保存到本地txt,这样查看更方便

from pywinauto import Application


app = Application('uia').start("notepad.exe")
win = app.window(title_re="无标题 - 记事本")
win.print_ctrl_ids(filename="x1.txt")

在windows上运行后文件写入的中文内容有乱码

windows 桌面GUI自动化- 18.pywinauto 保存控件菜单树结构print_control_identifiers()_PC端自动化 pywinauto

重新设保存文件默认编码可以解决此问题

from pywinauto import Application
import locale


def getpreferredencoding(do_setlocale = True):
    return "utf-8"


# 设置保存文件编码 "utf-8"
locale.getpreferredencoding = getpreferredencoding
print(locale.getpreferredencoding())

app = Application('uia').start("notepad.exe")
win = app.window(title_re="无标题 - 记事本")
win.print_ctrl_ids(filename="x1.txt")

windows 桌面GUI自动化- 18.pywinauto 保存控件菜单树结构print_control_identifiers()_控件_02



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  A32uB2Hhmc6N   2023年12月12日   51   0   0 MySQLMySQLideide
iKXeBmqdRgYF