![](/img/trans.png)
[英]How to Scroll down to click a particular element using Appium as scrollTo is not working
[英]How to scroll down to click the element in Android using appium and java?
我想知道如何使用 appium 和 java 向下滚动以单击 Android 中的元素?
我在“ android.support.v7.widget.RecyclerView
”中有一个元素列表。 由于它有超过 10 个元素,我们需要滑动屏幕才能看到下面的元素。 每个元素都有相同的 ID,即“ com.osanda.exampleapp/textViewTitle
”。 但是他们的文字是不同的,像“苹果”、“橙子”、“葡萄”……
我只需要滚动并使用其文本单击相关元素(“Apple”、“Orange”、“Grapes”......)
我遵循了许多教程,但无法正确完成。 我设法向下滚动屏幕。 但是当元素在滚动的中间 position 时就不行了。
当我列出元素名称时,它只显示可见元素,而不是所有元素。
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();
}
}
在新版本的 Appium 中你可以使用这个:
TouchActions action = new TouchActions(driver);
action.scroll(element, 10, 100);
action.perform();
element.click();
请使用下面的代码。 它将滚动直到文本可见。
String uiSelector = "new UiSelector().textMatches(\"" + text
+ "\")";
String command = "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView("
+ uiSelector + ");";
driver.findElementByAndroidUIAutomator(command);
现在您可以在此之后执行点击操作。
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();
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);
它将滚动直到文本可见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.