java 如何使用 Appium 向下滚动以单击特定元素,因为 scrollTo 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40291705/
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 a particular element using Appium as scrollTo is not working
提问by gaurav pandey
I'm trying to scroll down and click on some element in setting, but as scrollTo is not working I'm not able to proceed
我正在尝试向下滚动并单击设置中的某个元素,但由于 scrollTo 不起作用,我无法继续
package Test1;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.io.File;
import java.net.URL;
public class MobileFindJavaTest {
private AppiumDriver<WebElement> driver;
@BeforeMethod
@Test
public void setUp() throws Exception {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "/Apps/slide/");
File app = new File(appDir, "ApiDemos-debug.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName","BECUPJTWGA7HAQQK");
capabilities.setCapability("platformVersion", "5");
capabilities.setCapability("appPackage", "com.android.settings");
capabilities.setCapability("appActivity", ".Settings");
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), c Capabilities);
//driver.scrollTo("Views");
TouchAction touchAction4 = new TouchAction(driver);
touchAction4.press(30,40).moveTo(89,51).release();
driver.performTouchAction(touchAction4);
System.out.println("gaurav");
driver.findElementByName("Feature").click();
}
}
driver.scrollTo is showing not supported , now how to scroll down and click because the element which i want to click is not there on the screen , we need to scroll for it
driver.scrollTo 显示不受支持,现在如何向下滚动并单击,因为我要单击的元素不在屏幕上,我们需要滚动它
MobileElement radioGroup = (MobileElement) wd
.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()"
+ ".resourceId(\"com.android.settings:id/title\")).scrollIntoView("
+ "new UiSelector().text(\"Feature\"));");
radioGroup.click();
i tried in this way also but it is not scrolling...the setting opens and that's it
我也试过这种方式,但它没有滚动......设置打开,就是这样
采纳答案by Abhinav
You can scroll using below code snippet:
您可以使用以下代码片段滚动:
public void scroll() {
Dimension dimensions = driver.manage().window().getSize();
int Startpoint = (int) (dimensions.getHeight() * 0.5);
int scrollEnd = (int) (dimensions.getHeight() * 0.5);
driver.swipe(200, Startpoint,200,scrollEnd,2000);
}
You can have some condition upto which this method can be called and once your condition is met, scrolling will stop and you can perform desired action. Something like:
您可以有一些条件可以调用此方法,一旦满足您的条件,滚动将停止,您可以执行所需的操作。就像是:
while(condition){
scroll();
}
P.S.: You can modify value of 0.5 as required, I used the value as required in my case.
PS:您可以根据需要修改 0.5 的值,我在我的情况下根据需要使用了该值。
回答by shiv
// create a method name scrollTo or any name of your choice , with below mentioned code it will take parameter of text using which we want to scroll :
// 创建一个方法名称 scrollTo 或您选择的任何名称,使用下面提到的代码,它将采用我们想要滚动的文本参数:
public void scrollTo(String text)
{
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+text+"\").instance(0))");
}
回答by Megha Hulage
This works
这有效
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView("+ "new UiSelector().text(\"_TAGS_\"));");
回答by Daniel Raouf
Using wd driver for java script the following scroll method worked for me
将 wd 驱动程序用于 java 脚本,以下滚动方法对我有用
await driver.execute('mobile: scroll', {'direction': 'down'});
回答by Dnyanesh M
Below is the best one line solution I found for those people who are using Appium Java client version above 6--
以下是我为使用 6 以上 Appium Java 客户端版本的人找到的最好的单行解决方案-
driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+text+"\").instance(0))"));
回答by Star
For ScrollTo()
function the below method worked very well:
对于ScrollTo()
功能,以下方法效果很好:
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView("+ "new UiSelector().text(\"_input your text name here_\"));").click();
Please try using the above method & this should solve your problem with regards to scrollTo()
function, thanks.
请尝试使用上述方法,这应该可以解决您的scrollTo()
功能问题,谢谢。
回答by Mani
"Feature" text is display text or content description
“Feature”文本是显示文本或内容描述
if its display text :
如果它的显示文本:
MobileElement radioGroup = (MobileElement) wd
.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()"
+ ".resourceId(\"<listview_id>\")).scrollIntoView("
+ "new UiSelector().text(\"Feature\"));");
radioGroup.click();
This will scroll till the where the "Feature" text is displayed and click the "Feature" text. but one thing i need to explain you can scroll on only the view which has scroll functionality implemented not button, text etc..
这将滚动到显示“功能”文本的位置,然后单击“功能”文本。但是我需要解释的一件事是,您只能在具有滚动功能的视图上滚动,而不是按钮、文本等。
if its content description :
如果它的内容描述:
wd.scrollToExact("Feature")
For more info, please check this video :
欲了解更多信息,请查看此视频:
https://www.youtube.com/watch?v=bT3tqaLNn-Y
https://www.youtube.com/watch?v=bT3tqaLNn-Y
Click on Text:
点击文字:
MobileElement textLocartor = (MobileElement) wd
.findElementByAndroidUIAutomator("new UiSelector().text(\"<text_value>\")");
textLocartor.click();