Skip to content

AutomationBase and UIA facades

flaui.core.automation_base.AutomationBase

Bases: BaseModel

Delegates to a C# FlaUI.Core.AutomationBase instance (UIA2 or UIA3).

automation_type property

Return UIA2 or UIA3 for this automation stack.

coalesce_events property writable

Get coalesce-events option (C# enum).

condition_factory property

Return a Python wrapper around the C# ConditionFactory.

connection_recovery_behavior property writable

Get connection recovery behavior (C# enum).

connection_timeout property writable

Get the C# connection timeout (TimeSpan).

event_library property

Return the C# event identifier library.

mixed_attribute_value property

Return the sentinel for mixed text attributes.

not_supported_value property

Return the provider-specific sentinel for unsupported values.

overlay_manager property

Return a Python wrapper around the C# overlay manager (visual-debugging overlays).

pattern_library property

Return the C# pattern identifier library.

property_library property

Return the C# property identifier library.

text_attribute_library property

Return the C# text attribute library.

transaction_timeout property writable

Get the C# transaction timeout (TimeSpan).

tree_walker_factory property

Return the C# tree walker factory.

compare(element1, element2)

Return whether two elements refer to the same underlying UI element.

Source code in flaui/core/automation_base.py
@handle_csharp_exceptions
def compare(self, element1: Any, element2: Any) -> bool:
    """Return whether two elements refer to the same underlying UI element."""
    return self.raw_automation.Compare(
        self._to_cs_automation_element(element1),
        self._to_cs_automation_element(element2),
    )

dispose()

Release automation resources (matches C# Dispose).

Source code in flaui/core/automation_base.py
@handle_csharp_exceptions
def dispose(self) -> None:
    """Release automation resources (matches C# Dispose)."""
    self.raw_automation.Dispose()

focused_element()

Return the currently focused element, or None when the provider has no focus.

Source code in flaui/core/automation_base.py
@handle_csharp_exceptions
def focused_element(self) -> Optional[AutomationElement]:
    """Return the currently focused element, or None when the provider has no focus."""
    from flaui.core.automation_elements import AutomationElement as PyAutomationElement

    raw = self.raw_automation.FocusedElement()
    if raw is None:
        return None
    return PyAutomationElement(raw_element=raw)

from_handle(hwnd)

Return the element for a window handle (HWND).

Source code in flaui/core/automation_base.py
@handle_csharp_exceptions
def from_handle(self, hwnd: int) -> AutomationElement:
    """Return the element for a window handle (HWND)."""
    from System import IntPtr  # pyright: ignore
    from flaui.core.automation_elements import AutomationElement as PyAutomationElement

    return PyAutomationElement(raw_element=self.raw_automation.FromHandle(IntPtr(hwnd)))

from_point(point)

Return the element at the given screen point.

Source code in flaui/core/automation_base.py
@handle_csharp_exceptions
def from_point(self, point: Point) -> AutomationElement:
    """Return the element at the given screen point."""
    from flaui.core.automation_elements import AutomationElement as PyAutomationElement

    return PyAutomationElement(raw_element=self.raw_automation.FromPoint(point.raw_value))

get_desktop()

Return the desktop (root) element as a Python AutomationElement.

Source code in flaui/core/automation_base.py
@handle_csharp_exceptions
def get_desktop(self) -> AutomationElement:
    """Return the desktop (root) element as a Python AutomationElement."""
    from flaui.core.automation_elements import AutomationElement as PyAutomationElement

    return PyAutomationElement(raw_element=self.raw_automation.GetDesktop())

register_focus_changed_event(action)

Register a focus-changed handler.

Parameters:

Name Type Description Default
action Any

Callback (element) -> None invoked when focus changes.

required

Returns:

Type Description
Any

An :class:~flaui.core.event_handlers.EventRegistration handle that keeps the callback alive and can unregister it.

Source code in flaui/core/automation_base.py
@handle_csharp_exceptions
def register_focus_changed_event(self, action: Any) -> Any:
    """Register a focus-changed handler.

    :param action: Callback ``(element) -> None`` invoked when focus changes.
    :return: An :class:`~flaui.core.event_handlers.EventRegistration` handle that keeps the
        callback alive and can unregister it.
    """
    from flaui.core.automation_elements import AutomationElement as PyAutomationElement
    from flaui.core.event_handlers import EventRegistration, make_focus_changed_delegate, safe_invoke

    def _handler(sender: Any) -> None:
        """Bridge the C# callback to the Python action."""
        safe_invoke(action, PyAutomationElement(raw_element=sender))

    handler = self.raw_automation.RegisterFocusChangedEvent(make_focus_changed_delegate(_handler))
    return EventRegistration(
        cs_handler=handler,
        callback=_handler,
        unregister=lambda h: self.raw_automation.UnregisterFocusChangedEvent(h),
    )

unregister_all_events()

Remove all registered UI Automation event handlers.

Source code in flaui/core/automation_base.py
@handle_csharp_exceptions
def unregister_all_events(self) -> None:
    """Remove all registered UI Automation event handlers."""
    self.raw_automation.UnregisterAllEvents()

unregister_focus_changed_event(event_handler)

Unregister a focus-changed handler returned by :meth:register_focus_changed_event.

Parameters:

Name Type Description Default
event_handler Any

The :class:~flaui.core.event_handlers.EventRegistration returned by :meth:register_focus_changed_event, or a raw C# handler.

required
Source code in flaui/core/automation_base.py
@handle_csharp_exceptions
def unregister_focus_changed_event(self, event_handler: Any) -> None:
    """Unregister a focus-changed handler returned by :meth:`register_focus_changed_event`.

    :param event_handler: The :class:`~flaui.core.event_handlers.EventRegistration` returned by
        :meth:`register_focus_changed_event`, or a raw C# handler.
    """
    from flaui.core.event_handlers import EventRegistration

    if isinstance(event_handler, EventRegistration):
        event_handler.unregister()
    else:
        self.raw_automation.UnregisterFocusChangedEvent(event_handler)

validate_raw_automation(v, info) classmethod

Reject a missing C# automation reference.

Source code in flaui/core/automation_base.py
@field_validator("raw_automation")
@classmethod
def validate_raw_automation(cls, v: Any, info: ValidationInfo) -> Any:
    """Reject a missing C# automation reference."""
    if v is None:
        raise ValueError("raw_automation must not be None")
    return v

flaui.core.automation_base.wrap_cs_automation(raw)

Wrap an existing C# AutomationBase (e.g. from AutomationElement.Automation) in Python.

Parameters:

Name Type Description Default
raw Any

C# FlaUI.Core.AutomationBase implementation instance

required

Returns:

Type Description
AutomationBase

UIA2Automation or UIA3Automation Python wrapper

Raises:

Type Description
TypeError

If the object is not a supported automation implementation

ValueError

If raw is None

Source code in flaui/core/automation_base.py
def wrap_cs_automation(raw: Any) -> AutomationBase:
    """Wrap an existing C# AutomationBase (e.g. from AutomationElement.Automation) in Python.

    :param raw: C# FlaUI.Core.AutomationBase implementation instance
    :return: UIA2Automation or UIA3Automation Python wrapper
    :raises TypeError: If the object is not a supported automation implementation
    :raises ValueError: If raw is None
    """
    if raw is None:
        raise ValueError("automation reference is required")

    from FlaUI.UIA2 import UIA2Automation as CSUIA2Automation  # pyright: ignore
    from FlaUI.UIA3 import UIA3Automation as CSUIA3Automation  # pyright: ignore

    from flaui.uia2.automation import UIA2Automation
    from flaui.uia3.automation import UIA3Automation

    if isinstance(raw, CSUIA3Automation):
        return UIA3Automation(raw_automation=raw)
    if isinstance(raw, CSUIA2Automation):
        return UIA2Automation(raw_automation=raw)

    try:
        label = raw.AutomationType.ToString()
        if label == "UIA3":
            return UIA3Automation(raw_automation=raw)
        if label == "UIA2":
            return UIA2Automation(raw_automation=raw)
    except Exception:
        pass

    raise TypeError("Unsupported automation implementation: {!r}".format(type(raw)))

flaui.uia2.automation.UIA2Automation(raw_automation=None)

Bases: AutomationBase

UIA2 automation stack; wraps a C# FlaUI.UIA2.UIA2Automation instance.

Create a UIA2 wrapper, constructing C# UIA2Automation when raw_automation is omitted.

Parameters:

Name Type Description Default
raw_automation Optional[Any]

Existing C# UIA2Automation instance, or None to construct one

None
Source code in flaui/uia2/automation.py
def __init__(self, raw_automation: Optional[Any] = None) -> None:
    """Create a UIA2 wrapper, constructing C# UIA2Automation when raw_automation is omitted.

    :param raw_automation: Existing C# UIA2Automation instance, or None to construct one
    """
    raw = CSUIA2Automation() if raw_automation is None else raw_automation
    super().__init__(raw_automation=raw)

flaui.uia3.automation.UIA3Automation(raw_automation=None)

Bases: AutomationBase

UIA3 automation stack; wraps a C# FlaUI.UIA3.UIA3Automation instance.

Create a UIA3 wrapper, constructing C# UIA3Automation when raw_automation is omitted.

Parameters:

Name Type Description Default
raw_automation Optional[Any]

Existing C# UIA3Automation instance, or None to construct one

None
Source code in flaui/uia3/automation.py
def __init__(self, raw_automation: Optional[Any] = None) -> None:
    """Create a UIA3 wrapper, constructing C# UIA3Automation when raw_automation is omitted.

    :param raw_automation: Existing C# UIA3Automation instance, or None to construct one
    """
    raw = CSUIA3Automation() if raw_automation is None else raw_automation
    super().__init__(raw_automation=raw)