在 Appium 中,可以使用 CSS 选择器来查找 UI 元素。但是,CSS 选择器只支持精确匹配。如果要实现正则表达式模式匹配,可以使用 XPath 表达式。
以下是使用 XPath 表达式实现正则表达式模式匹配的示例代码:
// 根据元素属性值匹配正则表达式 driver.findElementsByXPath("//*[matches(@id, 'regex_\d+') and contains(@class, 'button')]");
// 根据元素文本内容匹配正则表达式 driver.findElementsByXPath("//*[matches(text(), 'regex.+pattern')]");
另外,还可以使用 Appium 的 By 自定义选择器来实现正则表达式模式匹配。例如:
// 根据元素属性值进行正则表达式匹配 driver.findElementByAndroidUIAutomator("new UiSelector().propertyMatch('id', /regex_\d+/)");
// 根据元素文本内容进行正则表达式匹配 driver.findElementByAndroidUIAutomator("new UiSelector().textMatches(/regex.+pattern/)");
需要注意的是,使用正则表达式模式匹配可能会影响性能。因此,在使用时需要慎重考虑。