[英]How to swipe till element is found in appium with using loop
我可以滑动直到找到元素,但随后出现 NoSuchElementException。
我写了以下代码。
int startX=(int) (dimension.width*0.5);
int startY=(int) (dimension.height*0.8);
int endX=(int) (dimension.width*0.2);
int endY=(int) (dimension.width*0.2);
TouchAction touch=new TouchAction(driver);
boolean b=false;
while (true) {
touch.press(PointOption.point(startX, startY)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2)))
.moveTo(PointOption.point(endX, endY)).release().perform();
Thread.sleep(5000);
try {
b=driver.findElement(By.xpath("//android.widget.TextView[@content-desc='Sweep']")).isDisplayed();
if(b) {
b=true;
break;
}
else {
continue;
}
}catch (Exception ex) {
ex.printStackTrace();
}
}
driver.findElement(By.xpath("//android.widget.TextView[@content-desc='Sweep']")).click();
使用基于 UIAutomator 的定位器会更可靠,因为您显然有一个可滚动的视图:
MobileElement element = (MobileElement) driver.findElement(MobileBy.AndroidUIAutomator(
"new UiScrollable(new UiSelector().scrollable(true))" +
".scrollIntoView(new UiSelector().textContains(\"Sweep\"))"));
它不仅会滚动到元素,还会确保它在视图端口区域内并且能够进行交互。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.