This Go library provides a powerful and high-performance interface for automating UI tasks on Windows using the UI Automation framework. It allows you to interact with and manipulate UI elements in applications through Windows COM interfaces.
- High Performance: Optimized with
IUIAutomationCacheRequestto batch fetch properties and patterns, reducing IPC (Inter-Process Communication) overhead significantly. - Reflection-Free: Removed reflection-based property population for improved runtime efficiency and better type safety.
- Improved Pattern Support: Ready-to-use wrappers for common Automation Patterns:
Value,Invoke,Toggle,ExpandCollapse, andSelectionItem. - Resource Safety: Strict COM object lifecycle management with explicit
Release()calls to prevent memory leaks and handle exhaustion. - Better Search API: Convenient methods like
FindByNameandFindByAutomationIdintegrated into theElementstructure.
go get -u github.com/auuunya/go-elementTo ensure proper operation on various Windows architectures (such as 32-bit or 64-bit versions of XP/7/10/11), please refer to the following build commands:
- 64-bit System (Default):
GOOS=windows GOARCH=amd64 go build . - 32-bit System (High Compatibility):
GOOS=windows GOARCH=386 go build .
Note: If you receive an
is not valid win32 applicationerror when running a 64-bit compiled program, please try rebuilding with the386architecture.
import uia "github.com/auuunya/go-element"
func main() {
uia.CoInitialize()
defer uia.CoUninitialize()
hwnd, _ := uia.GetWindowForString("Chrome_WidgetWin_1", "")
instance, _ := uia.CreateInstance(uia.CLSID_CUIAutomation, uia.IID_IUIAutomation, uia.CLSCTX_INPROC_SERVER)
ppv := uia.NewIUIAutomation(uia.NewIUnKnown(instance))
defer ppv.Release()
root, _ := uia.ElementFromHandle(ppv, hwnd)
defer root.Release()
// High-performance traversal using Cache Request
elems := uia.TraverseUIElementTree(ppv, root)
uia.TreeString(elems, 0)
}func main() {
uia.CoInitialize()
defer uia.CoUninitialize()
hwnd, _ := uia.GetWindowForString("Notepad", "")
instance, _ := uia.CreateInstance(uia.CLSID_CUIAutomation, uia.IID_IUIAutomation, uia.CLSCTX_ALL)
ppv := uia.NewIUIAutomation(uia.NewIUnKnown(instance))
defer ppv.Release()
root, _ := uia.ElementFromHandle(ppv, hwnd)
tree := uia.TraverseUIElementTree(ppv, root)
// Find element by name and set value via Pattern
if editor := tree.FindByName("文本编辑器"); editor != nil {
if vp, err := editor.GetValuePattern(); err == nil {
defer vp.Release()
vp.SetValue("Hello from go-element!")
}
}
}- High-performance property caching
- JSON serialization of UI structure
- Common Pattern wrappers (Invoke, Value, Toggle, etc.)
- UI Event listener support
- More specialized UI operations
Contributions are welcome! If you find a bug or want to enhance the library, feel free to open an issue or submit a pull request.
This library is distributed under the MIT License