Skip to content

"Find All Elements" issue with backslashes in Name or AutomationId #214

Open
@tpfahler

Description

@tpfahler

Example:
The address bar in a simple "open file" dialogue on a Windows 11 system has AutomationId 1001:
image
However if the selected directory is C:\Windows, the element Name is "Addresse: C:\Windows" and
FlaUILibrary.Find All Elements //*[@AutomationId="1001"]
will fail with
[ FAIL ] error: bad escape \W at position 19
because of the "\W" part in the Name property.
We had similar issues with AutomationIds which contain backslashes, e.g. "Table\1;1".

I think the root cause is in robotframework-flaui/src/.../util/automationelement.py, in the method _get_argument_in_xpath:
The implementation uses regex to build the xpath string properties for AutomationId, Name and ClassName, but the substitution is very likely to fail if the replacement value contains a backslash. The python doc states: "if it is a string, any backslash escapes in it are processed."
When we feed the name "Addresse: C:\Windows" to the python code,
re.sub(r"\[\d+]", "[@Name=\"Adresse: C:\\Windows\"]", "Foo[1]")
will throw exactly the same error message as the FlaUILibrary keyword in the example above.

I suggest the following fix, inspired by the comment at the end of the section in the python doc:

Replace line 34 in automationelement.py
xpaths[-1] = re.sub(r"\[\d+]", argument, xpaths[-1])
with
xpaths[-1] = re.sub(r"\[\d+]", argument.replace('\\',r'\\'), xpaths[-1])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions