HwndWrapper
Default wrapper for controls.
The HwndWrapper class is accessible via the pywinauto.controls.HwndWrapper module.
All other wrappers are derived from this.
This class wraps a lot of functionality of underlying windows API features for working with windows.
Most of the methods apply to every single window type. For example you can Click() on any window.
Most of the methods of this class are simple wrappers around API calls and as such they try do the simplest thing possible.
A HwndWrapper object can be passed directly to a ctypes wrapped C function - and it will get converted to a Long with the value of it's handle (see ctypes, _as_parameter_)
Attributes
Methods
f __init__(self, hwnd) ...
Initialize the control
- hwnd is either a valid window handle or it can be an instance or subclass of HwndWrapper.
If the handle is not valid then an InvalidWindowHandle error is raised.
f FriendlyClassName(self) ...
Return the friendly class name for the control
This differs from the class of the control in some cases. Class() is the actual 'Registered' window class of the control while FriendlyClassName() is hopefully something that will make more sense to the user.
For example Checkboxes are implemented as Buttons - so the class of a CheckBox is "Button" - but the friendly class is "CheckBox"
f WindowText(self) ...
Window text of the control
Quite a few contorls have other text that is visible, for example Edit controls usually have an empty string for WindowText but still have text displayed in the edit window.
f Style(self) ...
Returns the style of window
Return value is a long.
Combination of WS_* and specific control specific styles. See HwndWrapper.HasStyle() to easily check if the window has a particular style.
f ExStyle(self) ...
Returns the Extended style of window
Return value is a long.
Combination of WS_* and specific control specific styles. See HwndWrapper.HasStyle() to easily check if the window has a particular style.
f ControlID(self) ...
Return the ID of the window
Only controls have a valid ID - dialogs usually have no ID assigned.
The ID usually identified the control in the window - but there can be duplicate ID's for example lables in a dialog may have duplicate ID's.
f UserData(self) ...
Extra data associted with the window
This value is a long value that has been associated with the window and rarely has useful data (or at least data that you know the use of).
f IsUnicode(self) ...
Whether the window is unicode or not
A window is Unicode if it was registered by the Wide char version of RegisterClass(Ex).
f IsVisible(self) ...
Whether the window is visible or not
Checks that both the Top Level Parent (probably dialog) that owns this window and the window itself are both visible.
If you want to wait for a control to become visible (or wait for it to become hidden) use Application.Wait('visible') or Application.WaitNot('visible').
If you want to raise an exception immediately if a window is not visible then you can use the HwndWrapper.VerifyVisible(). HwndWrapper.VerifyActionable() raises if the window is not both visible and enabled.
f IsEnabled(self) ...
Whether the window is enabled or not
Checks that both the Top Level Parent (probably dialog) that owns this window and the window itself are both enabled.
If you want to wait for a control to become enabled (or wait for it to become disabled) use Application.Wait('visible') or Application.WaitNot('visible').
If you want to raise an exception immediately if a window is not enabled then you can use the HwndWrapper.VerifyEnabled(). HwndWrapper.VerifyReady() raises if the window is not both visible and enabled.
f Rectangle(self) ...
Return the rectangle of window
The rectangle is the rectangle of the control on the screen, coordinates are given from the top left of the screen.
This method returns a RECT structure, Which has attributes - top, left, right, bottom. and has methods width() and height(). See win32structures.RECT for more information.
f ClientRect(self) ...
Returns the client rectangle of window
The client rectangle is the window rectangle minus any borders that are not available to the control for drawing.
Both top and left are always 0 for this method.
This method returns a RECT structure, Which has attributes - top, left, right, bottom. and has methods width() and height(). See win32structures.RECT for more information.
f Font(self) ...
Return the font of the window
The font of the window is used to draw the text of that window. It is a structure which has attributes for Font name, height, width etc.
See win32structures.LOGFONTW for more information.
f Parent(self) ...
Return the parent of this control
Note that the parent of a control is not necesarily a dialog or other main window. A group box may be the parent of some radio buttons for example.
To get the main (or top level) window then use HwndWrapper.TopLevelParent().
f TopLevelParent(self) ...
Return the top level window of this control
The TopLevel parent is different from the parent in that the Parent is the window that owns this window - but it may not be a dialog/main window. For example most Comboboxes have an Edit. The ComboBox is the parent of the Edit control.
This will always return a valid window handle (if the control has no top level parent then the control itself is returned - as it is a top level window already!)
f Texts(self) ...
Return the text for each item of this control"
It is a list of strings for the control. It is frequently over-ridden to extract all strings from a control with multiple items.
It is always a list with one or more strings:
- First elemtent is the window text of the control
- Subsequent elements contain the text of any items of the control (e.g. items in a listbox/combobox, tabs in a tabcontrol)
f ClientRects(self) ...
Return the client rect for each item in this control
It is a list of rectangles for the control. It is frequently over-ridden to extract all rectangles from a control with multiple items.
It is always a list with one or more rectangles:
- First elemtent is the client rectangle of the control
- Subsequent elements contain the client rectangle of any items of the control (e.g. items in a listbox/combobox, tabs in a tabcontrol)
f Fonts(self) ...
Return the font for each item in this control
It is a list of fonts for the control. It is frequently over-ridden to extract all fonts from a control with multiple items.
It is always a list with one or more fonts:
- First elemtent is the control font
- Subsequent elements contain the font of any items of the control (e.g. items in a listbox/combobox, tabs in a tabcontrol)
f Children(self) ...
Return the children of this control as a list
It returns a list of HwndWrapper (or subclass) instances, it returns an empty list if there are no children.
f IsChild(self, parent) ...
Return True if this window is a child of 'parent'.
A window is a child of another window when it is a direct of the other window. A window is a direct descendant of a given window if the parent window is the the chain of parent windows for the child window.
f SendMessage(self, message, wparam=0, lparam=0) ...
Send a message to the control and wait for it to return
f SendMessageTimeout(self, message, wparam=0, lparam=0, timeout=None, timeoutflags=0) ...
Send a message to the control and wait for it to return or to timeout
If no timeout is given then a default timeout of .4 of a second will be used.
f PostMessage(self, message, wparam=0, lparam=0) ...
Post a message to the control message queue and return
f CaptureAsImage(self) ...
Return a PIL image of the control
See PIL documentation to know what you can do with the resulting image
f VerifyActionable(self) ...
Verify that the control is both visible and enabled
Raise either ControlNotEnalbed or ControlNotVisible if not enabled or visible respectively.
f VerifyEnabled(self) ...
Verify that the control is enabled
Check first if the control's parent is enabled (skip if no parent), then check if control itself is enabled.
f VerifyVisible(self) ...
Verify that the control is visible
Check first if the control's parent is visible. (skip if no parent), then check if control itself is visible.
f Click(self, button='left', pressed='', coords=(0, 0), double=False) ...
Simulates a mouse click on the control
This method sends WM_* messages to the control, to do a more 'realistic' mouse click use ClickInput() which uses SendInput() API to perform the click.
This method does not require that the control be visible on the screen (i.e. is can be hidden beneath another window and it will still work.)
f ClickInput(self, button='left', coords=(None, None), double=False, wheel_dist=0) ...
Click at the specified coordinates
- button The mouse button to click. One of 'left', 'right', 'middle' or 'x' (Default: 'left')
- coords The coordinates to click at.(Default: center of control)
- double Whether to perform a double click or not (Default: False)
- wheel_dist The distance to move the mouse week (default: 0)
This is different from Click in that it requires the control to be visible on the screen but performs a more realistic 'click' simulation.
This method is also vulnerable if the mouse if moved by the user as that could easily move the mouse off the control before the Click has finished.
f CloseClick(self, button='left', pressed='', coords=(0, 0), double=False) ...
Peform a click action that should make the window go away
The only difference from Click is that there are extra delays before and after the click action.
f DoubleClickInput(self, button='left', coords=(None, None)) ...
Double click at the specified coordinates
f PressMouseInput(self, button='left', coords=(None, None)) ...
Press a mouse button using SendInput
f DragMouse(self, button='left', pressed='', press_coords=(0, 0), release_coords=(0, 0)) ...
Drag the mouse
f TypeKeys(self, keys, pause=None, with_spaces=False, with_tabs=False, with_newlines=False, turn_off_numlock=True) ...
Type keys to the window using SendKeys
This uses the SendKeys python module from http://www.rutherfurd.net/python/sendkeys/ .This is the best place to find documentation on what to use for the keys
f DrawOutline(self, colour='green', thickness=2, fill=1, rect=None) ...
Draw an outline around the window
- colour can be either an integer or one of 'red', 'green', 'blue' (default 'green')
- thickness thickness of rectangle (default 2)
- fill how to fill in the rectangle (default BS_NULL)
- rect the coordinates of the rectangle to draw (defaults to the rectangle of the control.
f PopupWindow(self) ...
Return any owned Popups
Please do not use in production code yet - not tested fully
f Owner(self) ...
Return the owner window for the window if it exists
Returns None if there is no owner
f MenuItem(self, path) ...
Return the menu item specifed by path
Path can be a string in the form "MenuItem->MenuItem->MenuItem..." where each MenuItem is the text of an item at that level of the menu. E.g.
File->Export->ExportAsPNG
spaces are not important so you could also have written...
File -> Export -> Export As PNG
f MenuItems(self) ...
Return the menu items for the dialog
If there are no menu items then return an empty list
f MoveWindow(self, x=None, y=None, width=None, height=None, repaint=True) ...
Move the window to the new coordinates
- x Specifies the new left position of the window. Defaults to the current left position of the window.
- y Specifies the new top position of the window. Defaults to the current top position of the window.
- width Specifies the new width of the window. Defaults to the current width of the window.
- height Specifies the new height of the window. Default to the current height of the window.
- repaint Whether the window should be repainted or not. Defaults to True
f Close(self) ...
Close the window
Code modified from http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/
f GetShowState(self) ...
Get the show state and Maximized/minimzed/restored state
Returns a value that is a union of the following
- SW_HIDE the window is hidden.
- SW_MAXIMIZE the window is maximized
- SW_MINIMIZE the window is minimized
- SW_RESTORE the window is in the 'restored' state (neither minimized or maximized)
- SW_SHOW The window is not hidden
f SetFocus(self) ...
Set the focus to this control
Bring the window to the foreground first if necessary.
f SetApplicationData(self, appdata) ...
Application data is data from a previous run of the software
It is essential for running scripts written for one spoke language on a different spoken langauge
f Scroll(self, direction, amount, count=1) ...
Ask the control to scroll itself
direction can be any of "up", "down", "left", "right" amount can be one of "line", "page", "end" count (optional) the number of times to scroll
See the source for more information.