java 如何使用appium和java向下滚动以单击Android中的元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46550497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to scroll down to click the element in Android using appium and java?
提问by Osanda Deshan
I would like to know how to scroll down to click the element in Android using appium and java?
我想知道如何使用 appium 和 java 向下滚动以单击 Android 中的元素?
I am having a list of elements inside "android.support.v7.widget.RecyclerView
". Since it has more than 10 elements, we need to swipe the screen to see the below elements.
Each element has same id which is "com.osanda.exampleapp/textViewTitle
". But their texts are different like "Apple", "Orange", "Grapes"......
我在“ android.support.v7.widget.RecyclerView
”中有一个元素列表。由于它有10多个元素,我们需要滑动屏幕才能看到下面的元素。每个元素都有相同的 id,即“ com.osanda.exampleapp/textViewTitle
”。但他们的文字不同,如“Apple”、“Orange”、“Grapes”……
All I need is to scroll and click the relevant element using its text("Apple", "Orange", "Grapes".....)
我所需要的只是使用其文本滚动并单击相关元素(“Apple”,“Orange”,“Grapes”.....)
I have followed many tutorials but couldn't do it properly. I have managed to scroll down the screen. But it won't work when the element is in the middle position of the scroll.
我遵循了很多教程,但无法正确完成。我设法向下滚动屏幕。但是当元素位于滚动的中间位置时它不起作用。
When I listed the element names it only shows the visible elements, not all elements.
当我列出元素名称时,它只显示可见元素,而不是所有元素。
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());
}
Thank You.
谢谢。
回答by Osanda Deshan
This worked for me.
这对我有用。
public void scrollAndClick(String visibleText) {
androidDriver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+visibleText+"\").instance(0))").click();
}
}
回答by bsk
Please use the code below. It will scroll till the text is visible.
请使用下面的代码。它将滚动直到文本可见。
String uiSelector = "new UiSelector().textMatches(\"" + text
+ "\")";
String command = "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView("
+ uiSelector + ");";
driver.findElementByAndroidUIAutomator(command);
Now you could perform the click action after this.
现在您可以在此之后执行点击操作。
回答by Norayr Sargsyan
In new versions of Appium you can use this:
在新版本的 Appium 中你可以使用这个:
TouchActions action = new TouchActions(driver);
action.scroll(element, 10, 100);
action.perform();
element.click();
回答by ShiyamTJ
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;
}
In your test use as follows:
Example: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();
回答by Kiran Antony
Use the below while loop which looks for the contain by swiping down through the screen till the expected element appears
使用下面的 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);
}
once your element detected while exits and you can execute your action.
一旦您的元素在退出时检测到,您就可以执行您的操作。
回答by lokesh sharma
findElementByAndroidUIAutomator--AndroidUiAutomater
is depriciated in latest release,
findElementByAndroidUIAutomator--AndroidUiAutomater
在最新版本中被贬低,
Where as Now code which is working looks like
现在正在工作的代码看起来像
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();