Skip to content

XPath Navigator

Wraps the C# AutomationElementXPathNavigator for manual XPath-based navigation of the UI Automation tree. For most use cases prefer find_first_by_x_path / find_all_by_x_path; obtain a navigator with element.get_x_path_navigator().

flaui.core.xpath_navigator.AutomationElementXPathNavigator

Bases: BaseModel

Wraps the C# AutomationElementXPathNavigator for XPath-based tree navigation.

current_element property

Return the element at the navigator's current position.

Returns:

Type Description
'AutomationElement'

The current :class:~flaui.core.automation_elements.AutomationElement.

has_attributes property

Return whether the current node has attributes (True on element nodes).

Returns:

Type Description
bool

True when the current node is an element, False when positioned on an attribute.

is_empty_element property

Return whether the current node is an empty element (always False for UIA elements).

Returns:

Type Description
bool

The C# IsEmptyElement flag.

local_name property

Return the local name of the current node; equal to :attr:name since the tree has no namespaces.

Returns:

Type Description
str

The node's local name.

name property

Return the name of the current node (its control type, or attribute name).

Returns:

Type Description
str

The node name.

node_type property

Return the XPath node type of the current node (e.g. Root, Element).

Returns:

Type Description
str

The node type as a string.

underlying_object property

Return the element at the current position (C# UnderlyingObject; same as :attr:current_element).

Returns:

Type Description
'AutomationElement'

The current :class:~flaui.core.automation_elements.AutomationElement.

value property

Return the string value of the current node.

Returns:

Type Description
str

The node value.

clone()

Return an independent copy of this navigator at the same position.

Returns:

Type Description
'AutomationElementXPathNavigator'

A cloned navigator.

Source code in flaui/core/xpath_navigator.py
@handle_csharp_exceptions
def clone(self) -> "AutomationElementXPathNavigator":
    """Return an independent copy of this navigator at the same position.

    :return: A cloned navigator.
    """
    return AutomationElementXPathNavigator(raw_navigator=self.raw_navigator.Clone())

from_element(element) classmethod

Create a navigator rooted at the given element.

Parameters:

Name Type Description Default
element 'AutomationElement'

The element to use as the navigation root.

required

Returns:

Type Description
'AutomationElementXPathNavigator'

A new :class:AutomationElementXPathNavigator.

Source code in flaui/core/xpath_navigator.py
@classmethod
def from_element(cls, element: "AutomationElement") -> "AutomationElementXPathNavigator":
    """Create a navigator rooted at the given element.

    :param element: The element to use as the navigation root.
    :return: A new :class:`AutomationElementXPathNavigator`.
    """
    from FlaUI.Core import AutomationElementXPathNavigator as CSNavigator  # pyright: ignore

    return cls(raw_navigator=CSNavigator(element.raw_element))

get_attribute(name)

Return the value of a named attribute on the current element.

Supported attribute names mirror the C# navigator: AutomationId, Name, ClassName, HelpText, IsPassword, FullDescription, ItemType, AcceleratorKey, AccessKey, IsEnabled, IsOffscreen, ProcessId.

Parameters:

Name Type Description Default
name str

The attribute name.

required

Returns:

Type Description
Optional[str]

The attribute value, or an empty string when unset.

Source code in flaui/core/xpath_navigator.py
@handle_csharp_exceptions
def get_attribute(self, name: str) -> Optional[str]:
    """Return the value of a named attribute on the current element.

    Supported attribute names mirror the C# navigator: ``AutomationId``, ``Name``, ``ClassName``,
    ``HelpText``, ``IsPassword``, ``FullDescription``, ``ItemType``, ``AcceleratorKey``,
    ``AccessKey``, ``IsEnabled``, ``IsOffscreen``, ``ProcessId``.

    :param name: The attribute name.
    :return: The attribute value, or an empty string when unset.
    """
    return self.raw_navigator.GetAttribute(name, "")

is_same_position(other)

Return whether this navigator is at the same position as another.

Parameters:

Name Type Description Default
other 'AutomationElementXPathNavigator'

The navigator to compare against.

required

Returns:

Type Description
bool

True when both point at the same element (and attribute, if any).

Source code in flaui/core/xpath_navigator.py
@handle_csharp_exceptions
def is_same_position(self, other: "AutomationElementXPathNavigator") -> bool:
    """Return whether this navigator is at the same position as another.

    :param other: The navigator to compare against.
    :return: ``True`` when both point at the same element (and attribute, if any).
    """
    return self.raw_navigator.IsSamePosition(other.raw_navigator)

move_to(other)

Move to the same position as another navigator over the same tree.

Parameters:

Name Type Description Default
other 'AutomationElementXPathNavigator'

The navigator whose position to copy.

required

Returns:

Type Description
bool

True if moved, False if other is not an :class:AutomationElementXPathNavigator.

Source code in flaui/core/xpath_navigator.py
@handle_csharp_exceptions
def move_to(self, other: "AutomationElementXPathNavigator") -> bool:
    """Move to the same position as another navigator over the same tree.

    :param other: The navigator whose position to copy.
    :return: ``True`` if moved, ``False`` if ``other`` is not an :class:`AutomationElementXPathNavigator`.
    """
    return self.raw_navigator.MoveTo(other.raw_navigator)

move_to_attribute(local_name, namespace_uri='')

Move to the named attribute of the current element.

Parameters:

Name Type Description Default
local_name str

The attribute name (see :meth:get_attribute for the supported names).

required
namespace_uri str

The attribute namespace; the UIA tree uses none, so keep the default.

''

Returns:

Type Description
bool

True if moved, False if the attribute does not exist.

Source code in flaui/core/xpath_navigator.py
@handle_csharp_exceptions
def move_to_attribute(self, local_name: str, namespace_uri: str = "") -> bool:
    """Move to the named attribute of the current element.

    :param local_name: The attribute name (see :meth:`get_attribute` for the supported names).
    :param namespace_uri: The attribute namespace; the UIA tree uses none, so keep the default.
    :return: ``True`` if moved, ``False`` if the attribute does not exist.
    """
    return self.raw_navigator.MoveToAttribute(local_name, namespace_uri)

move_to_first_attribute()

Move to the first attribute of the current element.

While on an attribute, :attr:name is the attribute name and :attr:value its value; call :meth:move_to_parent to return to the element.

Returns:

Type Description
bool

True if moved, False if already on an attribute.

Source code in flaui/core/xpath_navigator.py
@handle_csharp_exceptions
def move_to_first_attribute(self) -> bool:
    """Move to the first attribute of the current element.

    While on an attribute, :attr:`name` is the attribute name and :attr:`value` its value; call
    :meth:`move_to_parent` to return to the element.

    :return: ``True`` if moved, ``False`` if already on an attribute.
    """
    return self.raw_navigator.MoveToFirstAttribute()

move_to_first_child()

Move to the first child of the current element.

Returns:

Type Description
bool

True if moved, False if there is no child.

Source code in flaui/core/xpath_navigator.py
@handle_csharp_exceptions
def move_to_first_child(self) -> bool:
    """Move to the first child of the current element.

    :return: ``True`` if moved, ``False`` if there is no child.
    """
    return self.raw_navigator.MoveToFirstChild()

move_to_next()

Move to the next sibling of the current element.

Returns:

Type Description
bool

True if moved, False if there is no next sibling.

Source code in flaui/core/xpath_navigator.py
@handle_csharp_exceptions
def move_to_next(self) -> bool:
    """Move to the next sibling of the current element.

    :return: ``True`` if moved, ``False`` if there is no next sibling.
    """
    return self.raw_navigator.MoveToNext()

move_to_next_attribute()

Move to the next attribute of the current element.

Returns:

Type Description
bool

True if moved, False when on the last attribute or not on an attribute.

Source code in flaui/core/xpath_navigator.py
@handle_csharp_exceptions
def move_to_next_attribute(self) -> bool:
    """Move to the next attribute of the current element.

    :return: ``True`` if moved, ``False`` when on the last attribute or not on an attribute.
    """
    return self.raw_navigator.MoveToNextAttribute()

move_to_parent()

Move to the parent of the current element.

Returns:

Type Description
bool

True if moved, False if already at the root.

Source code in flaui/core/xpath_navigator.py
@handle_csharp_exceptions
def move_to_parent(self) -> bool:
    """Move to the parent of the current element.

    :return: ``True`` if moved, ``False`` if already at the root.
    """
    return self.raw_navigator.MoveToParent()

move_to_previous()

Move to the previous sibling of the current element.

Returns:

Type Description
bool

True if moved, False if there is no previous sibling.

Source code in flaui/core/xpath_navigator.py
@handle_csharp_exceptions
def move_to_previous(self) -> bool:
    """Move to the previous sibling of the current element.

    :return: ``True`` if moved, ``False`` if there is no previous sibling.
    """
    return self.raw_navigator.MoveToPrevious()

move_to_root()

Move the navigator to the root element.

Source code in flaui/core/xpath_navigator.py
@handle_csharp_exceptions
def move_to_root(self) -> None:
    """Move the navigator to the root element."""
    self.raw_navigator.MoveToRoot()