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

本文介绍了如何使用.pywinauto库的print_control_identifiers方法来打印Windows应用程序的控件菜单树结构,以及处理文件中中文乱码问题的方法,通过设置文件编码为UTF-8。
摘要由CSDN通过智能技术生成

前言

.pywinauto 可以使用 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上运行后文件写入的中文内容有乱码

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

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")

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值