Python
[Python] UIAutomation for Windows 객체 컨트롤하기
jolocal
2024. 10. 22. 14:44
728x90
`Autoit`은 Python의 모듈로, Python을 통해서 window GUI를 컨트롤 할 수 있는 모듈이다. 모듈을 import한 후, `Autoit`프로그램을 통해서 window GUI들의 정보를 알아내어서 이를 기반으로 모듈의 명령문들로 핸들링하는 방식이다.
초기버전 windows의 GUI 객체 컨트롤은 `Autoit`으로 모두 가능했다. 하지만 windows의 버전이 올라감에 따라서 새로운 UI 요소를 그릴 수 있는 라이브러리가 공개되었다. 그것이 `.Net Framework` 이다. 현재 대부분의 창들은 모두 .Net Framework 3.0 이후의 버전을 사용해서 그려지고 있다. 이에 따라 MS에서는 .Net Framework의 GUI 객체 컨트롤을 위해서 `UIAutomation API`를 공개했다. 이번에 새롭게 사용해 봤고, 여기서 소개할 `Python UIAutomation-for-Windows`는 `UIAutomation API`를 Python에서 사용할 수 있도록 하게 해주는 라이브러리이다.
추가적으로, 현재 대부분의 RPA(Robotic Process Automation)툴들, 특히 windows 기반의 툴들은 `UIAutomation API`를 기반으로 만들어져있다고 한다.
기본적인 사용법
1. 설치
pip install uiautomation
2. 기본 import
import uiautomation as auto
3. 주요 사용법
a. 창 찾기
window = auto.WindowControl(ClassName = "클래스이름", Name = "창 이름")
b. 컨트롤 찾기
button = window.ButtonControl(Name="버튼 이름")
edit = window.EditControl(AutomationId="에디트 박스 ID")
c. 동작 수행
button.Click()
edit.SendKeys("텍스트 입력")
d.속성 확인
print(button.Name)
print(edit.GetValuePattern().Value)
e.대기
if window.Exists(5,1): # 5초 동안 1초 간격으로 확인
print("창이 존재합니다")
f.트리 항목 다루기
tree = window.TreeControl(Name="트리 이름")
item = tree.TreeItemControl(Name="항목 이름")
item.Expand()
g.목록 항목 다루기
list_control = window.ListControl(Name="리스트 이름")
item=list_control.ListItemContrl(Name="항목 이름")
item.Click()
h.스크롤
scroll_pattern = control.GetScarollPattern()
scroll_pattern.SetScrollPercent(0, 50) # 수직 50% 스크롤
i. 모든 자식 요소 열거
for child in window.GetChildren():
print(child.Name, child.ControlType)
j.특정 조건의 컨트롤 찾기
target = window.Control(searchDepth=1, ClassName="특정 클래스")
k.키보드입력
auto.SendKeys("{Ctrl}c") # Ctrl+C 입력
l.마우스 조작
auto.MoveTo(100, 100) # 마우스 이동
auto.Click(100, 100) # 마우스 클릭
m.스크린샷
control.CaptureToImage("screenshot.png")
728x90