xpath - 尝试通过 AndroidFindby(resourceid)、Appium 从 Parent(LinearLayout) 获取子节点(Switch)
问题描述
(class=android.widget.LinearLayout,resource-id=settings_language_selection_toggle,index=1 )
(class=android.widget.RelativeLayout,index=0)
(class=android.widget.LinearLayout,resource-id=widget_frame, index=1)
(class=android.widget.Switch,resource-id=switchWidget, index=0)
//////////// 你可以看看下面元素的分层表示
层次图像视图:
***我试图通过在appium Android for java上编写以下代码来达到开关按钮,但它不起作用
@AndroidFindBy(xpath ="new UiSelector().resourceId(\"com.idscan.mjcs.sample:id/settings_language_selection_toggle\").instance(1).getChildById(new UiSelector().className(\"android.widget.Switch\")
解决方案
您在 XPath 策略中使用 UiSelector 语法。这就是为什么它不起作用。试试这个:
@AndroidFindBy(uiAutomator = "resourceId(\"settings_language_selection_toggle\").childSelector(className(\"android.widget.Switch\"))")
正如您在此处看到的,可以省略一些样板,例如new UiSelector().resourceId(...)
可以简化为resourceId(...)
. 还有一件事:一旦找到根元素(具有给定 resourceId 的LinearLayout),就可以使用方法找到层次结构中的任何子元素.childSelector()
,嵌套无关紧要。
推荐阅读
- python - Sklearn NearestNeighbors (Mahalanobis) – 争论太多?
- database - 如何使用JDBC连接TDengine数据库?
- android - 如何在 Android Studio 中为 AcceleratDecelerateInterpolator 设置自定义函数
- opencv - 如何从背景中提取文档
- react-hooks - 在反应打字稿中使用 fetch 加载 products.json 文件
- flutter - 离开地图屏幕时禁用定位服务 (Flutter )
- javascript - 选择数据列表中的值后不显示占位符文本
- sql - 为什么 WHERE 子句中的 FIRST_VALUE 和 EXISTS 语句不过滤行?
- core-ui - 为什么在注释掉 coreui.bundle.min.js 后所有菜单都被禁用?
- css - [CSS][Youtube to OBS] 有没有办法忽略自定义消息/用户在流屏幕聊天中显示?