滚动的通用函数,其中 scrollablelist是我们必须在其中滚动的滚动视图的定位器
public void scrollToElementWithText(RemoteWebDriver driver, String scrollableList, String text) {
MobileElement element = (MobileElement) driver
.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().resourceId(\""
+ scrollableList + "\")).scrollIntoView(" + "new UiSelector().text(\"" + text + "\"))"));
}
您可以尝试使用现有代码scrollable(true).instance(0))
中的以下代码
如果将UiAutomator2
用作appium
引擎,请添加所需的功能automation
。>>
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
如果您具有元素的ID,现在使用下面的函数,并且页面上只有一个元素的索引为0。
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(); } }
使用文字滚动
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();
}
}