我想知道如何使用appium和java在Android中向下滚动以单击元素?
我在“
android.support.v7.widget.RecyclerView
”中有一个元素列表。由于它有超过10个元素,我们需要滑动屏幕才能看到下面的元素。
每个元素都有相同的 id,即“com.osanda.exampleapp/textViewTitle
”。但他们的文字不同,比如“苹果”、“橙子”、“葡萄”……
我需要的只是滚动并使用其文本单击相关元素(“Apple”,“Orange”,“Grapes”.....)
我已经遵循了很多教程,但无法正确执行。我已经成功地向下滚动屏幕。但当元素位于滚动的中间位置时,它将不起作用。
当我列出元素名称时,它仅显示可见元素,而不是所有元素。
List<WebElement> elements = androidDriver.findElementByClassName("android.support.v7.widget.RecyclerView").findElements(By.id("com.osanda.exampleapp:id/textViewTitle"));
for(WebElement element : elements) {
System.out.println(element.getText());
}
谢谢你。
我尝试了这个解决方案,它对我有用。
public void scrollAndClick(String visibleText) {
androidDriver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+visibleText+"\").instance(0))").click();
}
}
请使用以下代码。它将滚动直到文本可见。
String uiSelector = "new UiSelector().textMatches(\"" + text
+ "\")";
String command = "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView("
+ uiSelector + ");";
driver.findElementByAndroidUIAutomator(command);
现在您可以在此之后执行点击操作。
在新版本的 Appium 中你可以使用这个:
TouchActions action = new TouchActions(driver);
action.scroll(element, 10, 100);
action.perform();
element.click();
public AndroidElement ScrollToElement(String by, String using) {
AndroidElement element = null;
int numberOfTimes = 10;
Dimension size = driver.manage().window().getSize();
int anchor = (int) (size.width / 2);
// Swipe up to scroll down
int startPoint = (int) (size.height - 10);
int endPoint = 10;
for (int i = 0; i < numberOfTimes; i++) {
try {
new TouchAction(driver)
.longPress(point(anchor, startPoint))
.moveTo(point(anchor, endPoint))
.release()
.perform();
element = (AndroidElement) driver.findElement(by, using);
i = numberOfTimes;
} catch (NoSuchElementException ex) {
System.out.println(String.format("Element not available. Scrolling (%s) times...", i + 1));
}
}
return element;
}
在您的测试中使用如下: 示例:
String usingWebView = “//android.widget.TextView[@text=‘Spinner’]”;
String by = “xpath”;
MobileActions mbAct = new MobileActions(driver); //This is just a class which has above function code.
AndroidElement webViewElement = mbAct.ScrollToElement(by, usingWebView, 250);
webViewElement.click();
findElementByAndroidUIAutomator--AndroidUiAutomater
在最新版本中已降价,
现在正在运行的代码看起来像这样
MobileElement element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiScrollable(new UiSelector().resourceId(\"com.android.vending:id/tab_recycler_view\")).getChildByText("
+ "new UiSelector().className(\"android.widget.TextView\"), \"Games We Are Playing\")"));
//Perform the action on the element
element.click();
使用下面的 while 循环,通过向下滑动屏幕直到出现预期元素来查找包含内容
While (driver.findElements(By.id(“id”)).size()==0){
size = driver.manage().window().getSize();
int starty = (int) (size.height * 0.80);
int endy = (int) (size.height * 0.20);
int startx = size.width / 2;
System.out.println("starty = " + starty + " ,endy = " + endy + " , startx = " + startx);
driver.swipe(startx, starty, startx, endy, 3000);
Thread.sleep(2000);
driver.swipe(startx, endy, startx, starty, 3000);
Thread.sleep(2000);
}
一旦在退出时检测到您的元素,您就可以执行您的操作。
这对我有用,当文本可见时滚动将停止。这里我的文本是:“进度条”,只需根据您的文本进行替换即可。它会起作用的。
String scrollElement = "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\"Progress Bar\").instance(0))";
driver.findElementByAndroidUIAutomator(scrollElement).click();
你可以用这个
public void scrollAndClick(String visibleText) {
androidDriver.findElementByAndroidUIAutomator
("new UiScrollable(new UiSelector().scrollable(true).
instance(0)).scrollIntoView(
new UiSelector().textContains(\""+visibleText+
"\").instance(0))").click();
}
}
或者这个:
String uiSelector = "new UiSelector().textMatches
(\"" + text+ "\")";
String command = "new UiScrollable(
new UiSelector().scrollable(true).
instance(0)).scrollIntoView("+ uiSelector + ");";
driver.findElementByAndroidUIAutomator(command);
它将滚动直到文本可见。
如果您使用的是 Appium Java 客户端版本 6 以上,则无法在 Appium 驱动程序上使用
findElementByAndroidUIAutomator
方法。 MobileBy
也已被弃用,因此今天可行的解决方案是:
public void scrollAndClick(String visibleText) {
androidDriver.findElement(AppiumBy.androidUIAutomator(
"new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+visibleText+"\").instance(0))")).click();
}
}