Giter VIP home page Giter VIP logo

python-uiautomation-for-windows's Introduction

The uiautomation module

This module is for UIAutomatoin on Windows(Windows XP with SP3, Windows Vista, Windows 7 and Windows 8/8.1/10). It supports UIAutomatoin for the applications which implmented UIAutomation Provider, such as MFC, Windows Form, WPF, Modern UI(Metro UI), Qt and Firefox.

uiautomation is shared under the Apache Licence 2.0. This means that the code can be freely copied and distributed, and costs nothing to use.

Only uiautomation.py and the dll files are needed for UIAutomation. Other scripts are all demos. You can install uiautomation by "pip install uiautomation"

Run 'uiautomation.py -h' for help. Run automate_notepad_py3.py to see a simple demo.

If "RuntimeError: Can not get an instance of IUIAutomation" occured when running uiautomation.py, You need to install update KB971513 for your Windows. You can also download it from here https://github.com/yinkaisheng/WindowsUpdateKB971513ForIUIAutomation

By the way, You'd better run python as administrator. Otherwise uiautomation may fail to enumerate controls under some circumstances.

Requirements:

Microsoft UIAutomation Minimum supported client: Windows 7, Windows Vista with SP2 and Platform Update for Windows Vista, Windows XP with SP3 and Platform Update for Windows Vista [desktop apps only]

Microsoft UIAutomation Minimum supported server: Windows Server 2008 R2, Windows Server 2008 with SP2 and Platform Update for Windows Server 2008, Windows Server 2003 with SP2 and Platform Update for Windows Server 2008 [desktop apps only]


How to use uiautomation? run 'automation.py -h' or 'uiautomation.py -h' help Understand the arguments of uiautomation, and try the following examples
uiautomation.py -r -d 1 -t 0, print desktop(the root of control tree) and it's children(top level windows)
uiautomation.py -t 0, print current active window's controls

run notepad.exe, run uiautomation.py -t 3, swith to Notepad and wait for 5 seconds

uiautomation will print the controls of Notepad and save them to @AutomationLog.txt:

ControlType: WindowControl ClassName: Notepad
  ControlType: EditControl ClassName: Edit
    ControlType: ScrollBarControl ClassName:
      ControlType: ButtonControl ClassName:
      ControlType: ButtonControl ClassName:
    ControlType: ScrollBarControl ClassName:
      ControlType: ButtonControl ClassName:
      ControlType: ButtonControl ClassName:
    ControlType: ThumbControl ClassName:
  ControlType: StatusBarControl ClassName:
    ControlType: TextControl ClassName:
    ControlType: TextControl ClassName:
...

run the following code

import subprocess
import uiautomation as automation

print(automation.GetRootControl())
subprocess.Popen('notepad.exe')
notepadWindow = automation.WindowControl(searchDepth = 1, ClassName = 'Notepad')
print(notepadWindow.Name)
notepadWindow.SetTopmost(True)
edit = notepadWindow.EditControl()
edit.SetValue('Hello')
edit.SendKeys('{Ctrl}{End}{Enter}World')

automation.GetRootControl() returns the root control
automation.WindowControl(searchDepth = 1, ClassName = 'Notepad') creates a WindowControl, the parameters specify how to search the control
the following parameters can be used
searchFromControl = None,
searchDepth = 0xFFFFFFFF,
searchWaitTime = SEARCH_INTERVAL,
foundIndex = 1
Name
ClassName
AutomationId
ControlType
Depth

See Control.__init__ for the comment of the parameters
See automation_notepad_py3.py for a detailed example


Another UI tool inspectX86.exe or inspectX64.exe supplied by Microsoft can also be used to see the UI elements.

Inspect (Inspect.exe) is a Windows-based tool that enables you select any UI element and view the element's accessibility data. You can view Microsoft UI Automation properties and control patterns, as well as Microsoft Active Accessibility properties. Inspect also enables you to test the navigational structure of the automation elements in the UI Automation tree, and the accessible objects in the Microsoft Active Accessibility hierarchy.

Inspect is installed with the Windows Software Development Kit (SDK) for Windows 8. (It is also available in previous versions of Windows SDK.) It is located in the \bin<platform> folder of the SDK installation path (Inspect.exe).

Inspect Inspect

代码原理简单介绍

Some screenshots:

WindowsDesktop Desktop

Qt5 Qt5

Firefox Firefox

Wireshark(version must >= 2.0) Wireshark

QQ QQ

Batch rename pdf bookmark bookmark

python-uiautomation-for-windows's People

Contributors

yinkaisheng avatar kakinumacn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.