[英]scrollIntoView not working in appium for log scroll view. Throws no such element exception after one/two swipes
Scroll 的通用函數,其中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))
如果您使用appium
作為automation
引擎,請添加所需的功能UiAutomator2
。
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();
}
}
使用屏幕尺寸幫助滾動:
public void scrollToBottom() {
int x = driver.manage().window().getSize().width / 2;
int start_y = (int) (driver.manage().window().getSize().height * 0.2);
int end_y = (int) (driver.manage().window().getSize().height * 0.8);
TouchAction dragNDrop = new TouchAction(driver)
.press(PointOption.point(x,start_y)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500)))
.moveTo(PointOption.point(x, end_y))
.release();
dragNDrop.perform();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.