python之GUI自动化uiautomation使用

uiautomation模块的自动化测试需要配合UISPY或者inspect工具来进行调试,推荐使用UISPY,具体使用看自己。
UISPY简单介绍:
在这里插入图片描述
常用类型元素:


程序窗口:WindowControl()

按钮:ButtonControl()		

文件显示:TextControl()

输入框:EditControl()


常用定位属性:


ClassName、Name、ProcessId、AutomationId


常用操作:

# DoubleClick() 双击
# Click() 点击;
# RighClik() 右键点击;
# SendKeys() 发送字符;
# SetValue() 传值,一般对EditControl用;

uiautomation模块是第三方模块需要先安装

pip install uiautomation

安装完毕后,下面是计算器定位操作的代码:

import os
import subprocess
import uiautomation
import time
subprocess.Popen('calc.exe')
time.sleep(2)
wc=uiautomation.WindowControl(searchDepth=1,Name='计算器')
#设置为顶层
wc.SetTopmost(True)
wc.ButtonControl(Name='六').Click()
wc.ButtonControl(Name='加').Click()
wc.ButtonControl(Name='六').Click()
wc.ButtonControl(Name='等于').Click()
# 获取元素某标签的具体标签内容
result=wc.TextControl(AutomationId='CalculatorResults')
print(result.Name)
print(result.ClassName)
print(result.ProcessId)
print(result.AutomationId)
print(result.Culture)
print(result.ControlType)

if result.Name=='显示为 12':
    print('测试成功success')
else:
    print('测试失败error')
#进行截图
wc.CaptureToImage('test.png')
time.sleep(2)
wc.ButtonControl(Name='关闭 计算器').Click() #name可能会有变化,具体情况按照上面打印的Name进行填写
os.system("taskkill /F /IM calc.exe")

回显代码:

显示为 12

15100
CalculatorResults
2052
50020
测试成功success
错误: 没有找到进程 "calc.exe"
  • 2
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python uiautomation 是一个基于 Windows UI 自动化库的 Python 库,可以用于自动化测试、GUI 自动化等场景。以下是使用 Python uiautomation 的详细步骤: 1. 安装 Python uiautomation 可以使用 pip 命令来安装 Python uiautomation: ``` pip install uiautomation ``` 2. 导入 uiautomation 库 在 Python 代码中导入 uiautomation 库: ```python import uiautomation as auto ``` 3. 查找 UI 元素 使用 uiautomation 库提供的方法查找 UI 元素,比如通过元素名称、元素类型、元素位置等方式查找元素: ```python # 通过元素名称查找元素 element = auto.WindowControl(searchDepth=1, ClassName='Chrome_WidgetWin_1', SubName='Google Chrome') # 通过元素类型查找元素 element = auto.ButtonControl(searchDepth=2, ClassName='Button', AutomationId='123') # 通过元素位置查找元素 element = auto.WindowControl(searchFromControl=desktop, AutomationId='Desktop') ``` 4. 操作 UI 元素 使用 uiautomation 库提供的方法操作 UI 元素,比如单击、双击、输入文本等操作: ```python # 单击元素 element.Click() # 双击元素 element.DoubleClick() # 输入文本 element.SendKeys('hello world') ``` 5. 获取 UI 元素属性 使用 uiautomation 库提供的方法获取 UI 元素的属性,比如元素名称、元素类型、元素位置等: ```python # 获取元素名称 name = element.Name # 获取元素类型 controlType = element.ControlType # 获取元素位置 rect = element.BoundingRectangle ``` 以上就是使用 Python uiautomation 库的详细步骤。需要注意的是,uiautomation 库只能在 Windows 操作系统上使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值