android - 我无法在我的移动应用自动化框架中滚动和选择 Appium android 中的元素。?
问题描述
这是我的代码快照。此代码将选择元素,但不会滚动到确切的元素并选择值。请帮我修改一下?
public void applyBedTimefromdate() throws InterruptedException {
MobileElement element = (MobileElement) driver
.findElementsByAndroidUIAutomator(
"new UiScrollable(new UiSelector().resourceId(\"com.hdc:id/hour\")).getChildByText("
+ "new UiSelector().className(\"android.view.View[@index='0']\"), \"3\"));");
element.click();
}
解决方案
我们可以尝试使用 UiAutomator2 按文本滚动。
如果您使用的是 appium,请添加所需的功能“UiAutomator2”。
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
现在试试下面的代码。
public void scrollByText(String menuText) {
try {
driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches(\"" + menuText + "\").instance(0));"));
} catch (Exception e) {
e.printStackTrace();
}
}
如果您有元素的 id(资源 id),请使用以下函数
public void scrollByID(String Id, int index) {
try {
driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().resourceId(\""+Id+"\").instance("+index+"));"));
} catch (Exception e) {
e.printStackTrace();
}
}
推荐阅读
- html - 菜单活动列表项高度
- rest - 如何使用 MVC Web Api GET 将对象作为参数传递
- python - Flask Heroku 部署的 Librosa 安装:“OSError: sndfile library not found”(H10 错误)
- php - 尝试读取 null 上的属性
- json - 将json转换为dart时遇到问题,下面是json
- ios - 允许应用程序在 iOS Swift 中在后台运行
- python - 如何计算数据框中预测标签的百分比?
- http - 我们可以在 UMDF 驱动程序中成功调用 winhttp 请求吗?
- tensorflow - 从功能模型中删除重新缩放层
- python-3.x - ValueError:信号仅在主线程库中有效:线程