robocorp / rpaframework

Collection of open-source libraries and tools for Robotic Process Automation (RPA), designed to be used with both Robot Framework and Python
https://www.rpaframework.org/
Apache License 2.0
1.17k stars 227 forks source link

Add "element path" type locator strategy to the `RPA.Windows` library #820

Closed mikahanninen closed 1 year ago

mikahanninen commented 1 year ago

Robot Framework usage example

*** Settings ***
Library     ExtendedWindows.py

*** Tasks ***
Minimal task
    Control Window    name:Calculator
    FOR    ${idx}    IN RANGE    2    11
        ${element}=    Get Element With Path    2|3|2|8|2|2|5|${idx}
        IF    ${idx}==2    Log To Console    ${{dir($element.item)}}
        Click    ${element}
    END
    Log    Done.

Custom Python library implementation

from RPA.Windows import Windows
from RPA.core.windows.locators import WindowsElement
from robot.api.deco import keyword
import logging

class ExtendedWindows(Windows):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    @keyword
    def get_element_with_path(self, element_path: str, log_elements: bool = False) -> WindowsElement:
        root = self.window_element.item
        element_index_path = [item.strip() for item in element_path.split("|")]
        child_elements = []
        target = None
        for idx in element_index_path:
            child_idx = int(idx) - 1
            if log_elements:
                logging.warning(root)
            child_elements = root.GetChildren()
            if child_idx < len(child_elements):
                root = child_elements[child_idx]
                root.robocorp_click_offset = None
                target = WindowsElement(root, element_path)
        return target

An example how element path indexes are calculate

image

cmin764 commented 1 year ago

Relates to: https://github.com/robocorp/rpaframework/issues/779