uiautomation函数讲解(下)
🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝🔝
🥰 博客首页:knighthood2001
😗 欢迎点赞👍评论🗨️
❤️ 热爱python,期待与大家一同进步成长!!❤️
👀给大家推荐一款很火爆的刷题、面试求职网站👀
跟我一起来巩固基础和刷题吧
目录
获取光标位置
设置光标位置
win32gui获取句柄
切换窗口(需要知道句柄)
是否顶级窗口
窗口是否可见
获取窗口文本
设置窗口文本
获取编辑文本
获取系统剪贴板当前具有的剪贴板格式
获取控件(详)
获取父控件
获取第一个子控件
获取最后一个子控件
获取下一个兄弟控件
获取先前的兄弟控件
获取子控件
设置全局搜索超时
WalkControl()
显示桌面
获取光标位置
使用GetCursorPos()和GetPhysicalCursorPos()函数,后者是获取物理光标位置。
import uiautomation as autoa = auto.GetCursorPos()print(a)b = auto.GetPhysicalCursorPos()print(b)
结果如下:
(965, 534)(965, 534)
设置光标位置
c = auto.SetCursorPos(800, 700)print(c)# 结果为True
将鼠标光标设置为点 x,y。
x:int
y:int
返回 bool,如果成功则返回 True,否则返回 False。
win32gui获取句柄
import win32guihwnd_title = dict()def get_all_hwnd(hwnd, arg): if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd): hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)})win32gui.EnumWindows(get_all_hwnd, 0)for h, t in hwnd_title.items(): if t != "": print(h, t)
运行结果如下:
切换窗口(需要知道句柄)
SwitchToThisWindow(handle: int) -> None
是否顶级窗口
IsTopLevelWindow(handle: int) -> bool
窗口是否可见
IsWindowVisible(handle: int) -> bool
获取窗口文本
GetWindowText(handle: int) -> str
如:
m = auto.GetWindowText(1182628)print(m)# 微信
该句柄是微信的句柄
设置窗口文本
SetWindowText(handle: int, text: str) -> bool
例如:你先重新设置该句柄所对应的窗口文本,然后获取出来,就改变了。(也适用于没有窗口名称,通过这样自定义)
auto.SetWindowText(1182628, 'QQ')m = auto.GetWindowText(1182628)print(m)# QQ
获取编辑文本
GetEditText(handle: int) -> str
获取系统剪贴板当前具有的剪贴板格式
GetClipboardFormats() -> Dict[int, str]
结果如下:
{49386: 'HTML Format', 49295: 'Rich Text Format', 13: 'CF_UNICODETEXT', 1: 'CF_TEXT', 49391: 'UniformResourceLocator', 50023: 'JAVA_DATAFLAVOR:application/x-java-jvm-local-objectref; class=c', 16: 'CF_LOCALE', 7: 'CF_OEMTEXT'}
获取控件(详)
获取父控件
GetParentControl(self) -> 'Control'
获取第一个子控件
GetFirstChildControl(self) -> 'Control'
获取最后一个子控件
GetLastChildControl(self) -> 'Control'
获取下一个兄弟控件
GetNextSiblingControl(self) -> 'Control'
获取先前的兄弟控件
GetPreviousSiblingControl(self) -> 'Control'
获取子控件
GetChildren(self) -> List['Control']
例如:
wechatWindow = auto.WindowControl(searchDepth=1, className='WeChatMainWndForPC', Name='微信')print(wechatWindow.GetFirstChildControl())
结果为
ControlType: PaneControl ClassName: Shell_TrayWnd AutomationId: Rect: (0,1030,1920,1080)[1920x50] Name: '任务栏' Handle: 0x1010C(65804)
不同的控件下,又会有不同的属性,需要自己根据去选择。
设置全局搜索超时
SetGlobalSearchTimeout(seconds: float) -> None
为了使它全局有效,将它放在导入uiautomation后
import uiautomation as autoauto.SetGlobalSearchTimeout(1)
WalkControl()
WalkControl(control: Control, includeTop: bool = False, maxDepth: int = 0xFFFFFFFF)
control:“控制”或其子类。 includeTop:bool,如果为True,则先yield(control, 0)。 maxDepth:int,枚举深度。 Yield 2 items tuple (control: Control, depth: int).产生 2 个项目元组(控制:控制,深度:int)。
例如:
for c, d in auto.WalkControl(control, maxDepth=6): # 判断是否为这种类型 if c.ControlType not in [auto.ControlType.EditControl, auto.ControlType.TextControl]: continue print(c.Name)
显示桌面
ShowDesktop(waitTime: float = 1)
其通过按win + d显示桌面