我在滚动 android 时使用以下方法。但它并不总是滚动到同一位置,即使它位于同一页面上。会是什么原因。
public void ScrollToCampaignNotice(){
try {
getDriver().findElement(AppiumBy.androidUIAutomator(
"new UiScrollable(new UiSelector().scrollable(true)).flingToEnd(35)"));
} catch (InvalidSelectorException e) {
// ignore
}
}
同时,我尝试了以下方法,也有同样的问题。
您可以滚动到确切的元素,而不是滑动 35 次。
以下是如何滚动到可见的特定文本的示例:
new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains("<include your text>").instance(0))
您还可以滚动到具有特定 id 的元素,如下所示:
new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().resourceId("<insert element ID here>").instance(0))
使用最新的 android UiAutomator2 移动命令,并记住它仅适用于 UiSelector(最佳)、辅助功能 ID 和类名称。
HashMap<String, String> map1 = new HashMap<>();
map1.put("strategy", "-android uiautomator");
map1.put("selector", "UiSelector().text(\"Art of Asia\")");
driver.executeScript("mobile: scroll", map1);
driver.findElement(AppiumBy.androidUIAutomator("UiSelector().text(\"Art of Asia\")")).click();