At AcmeQA, UI automation tests often receive multiple candidate selectors for each required page element. Some selectors are unstable because they contain dynamic tokens such as generated IDs or session values. Implement an algorithm that chooses the most stable selector for each element.
Given a list of selector groups, where each group contains candidate selector strings for one UI element, return one chosen selector per group. A selector is more stable if it has fewer dynamic tokens. A token is dynamic if it contains at least one digit. If multiple selectors in the same group have the same number of dynamic tokens, choose the one with fewer total tokens. If there is still a tie, choose the lexicographically smallest selector.
A selector is split into tokens by the character > and each token should be trimmed before evaluation.
selector_groups, a list of lists of stringsith string is the chosen selector for selector_groups[i]Example 1
[["div>button#submit123", "div>button.primary"], ["form>input[name=email]", "form>input#field99"]]["div>button.primary", "form>input[name=email]"]Example 2
[["app>div.card>span", "app>div2>span"], ["ul>li.item", "ul>li.item2"]]["app>div.card>span", "ul>li.item"]1 <= len(selector_groups) <= 10^41 <= len(selector_groups[i]) <= 1001 <= len(selector) <= 200