UiPath Pitfall #1: Relying on idx in Your Selectors

UiPath Pitfall #1: Relying on idx in Your Selectors

如果你的选择器在某些运行中有效,而在另一些运行中失败,请打开它并查找 idx 属性。十有八九,问题就在这里。我在 UiPath 论坛上经常看到这个问题,所以它是我“每周陷阱”系列中写的第一篇文章。

Why idx breaks When you indicate an element and Studio can’t tell it apart from similar elements, it adds something like idx=‘3’ to the selector. That works on your screen today. But idx is just the element’s position in the UI hierarchy, nothing more. Add a column to a table, get a popup, or have the page load a little differently, and position 3 is now a different element. Or nothing at all. The robot either clicks the wrong thing or times out, and it looks random because it depends on what the screen looked like on that run.

idx 为什么会失效?当你指定一个元素而 Studio 无法将其与相似元素区分开时,它会在选择器中添加类似 idx=‘3’ 的内容。这在今天的屏幕上确实有效,但 idx 仅仅是元素在 UI 层级中的位置,仅此而已。如果在表格中增加一列、弹出一个窗口,或者页面加载方式稍有不同,位置 3 就变成了另一个元素,甚至什么都没有。机器人要么点击了错误的东西,要么超时,由于这取决于该次运行时的屏幕状态,因此看起来就像是随机发生的。

UiPath’s own Workflow Analyzer rule for this, UI-REL-001 (Large Idx in Selectors), says the same thing: the order idx relies on can change when the elements you’re automating change, so it should be avoided. The fix Open the selector in UI Explorer and replace idx with something that actually describes the element. Good candidates: aaname id automationid innertext

UiPath 自身的流程分析器规则 UI-REL-001(选择器中的大索引值)也指出了这一点:当自动化元素发生变化时,idx 所依赖的顺序可能会改变,因此应避免使用它。解决方法是:在 UI Explorer 中打开选择器,用真正能描述该元素的属性替换 idx。好的候选属性包括:aaname、id、automationid、innertext。

So instead of this: you’d aim for something like this: If the stable value changes a bit between runs, use a wildcard. * replaces zero or more characters:

因此,与其使用 ,不如尝试类似 的写法。如果稳定值在不同运行之间有细微变化,请使用通配符。* 可以代表零个或多个字符:。

In Modern activities, add an anchor, meaning a nearby label that never moves, so the target is found relative to something stable. Then keep Fuzzy selector or Image as a fallback targeting method in case the strict selector misses.

在现代活动中,添加一个锚点,即附近一个从不移动的标签,这样目标就可以相对于稳定的参照物被找到。然后,保留模糊选择器或图像作为后备定位方法,以防精确选择器失效。

A quick check before you publish Search your project for idx=. Every hit is a possible failure in production. You can also let Workflow Analyzer do it for you. UI-REL-001 lists every selector with an idx value above the threshold you set, one by one, so you can work through them before they bite.

发布前的快速检查:在你的项目中搜索 idx=。每一个匹配项都可能是生产环境中的潜在故障点。你也可以让流程分析器为你完成这项工作。UI-REL-001 会逐一列出所有 idx 值超过你所设阈值的选择器,这样你就可以在它们引发问题之前将其解决。