代码之家  ›  专栏  ›  技术社区  ›  Osanda Deshan

如何使用appium和java向下滚动以单击Android中的元素?

  •  7
  • Osanda Deshan  · 技术社区  · 8 年前

    我在里面有一个元素列表“ 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());
            }
    

    8 回复  |  直到 8 年前
        1
  •  17
  •   Abhinav Gupta Eduardo Oliveira    5 年前

    我尝试了这个解决方案,它对我有效。

    public void scrollAndClick(String visibleText) {
         androidDriver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+visibleText+"\").instance(0))").click();
            }
        }
    
        2
  •  3
  •   bsk    8 年前

    请使用下面的代码。它将滚动直到文本可见。

       String uiSelector = "new UiSelector().textMatches(\"" + text
                            + "\")";
    
       String command = "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView("
                            + uiSelector + ");";
    
        driver.findElementByAndroidUIAutomator(command);
    

    现在,您可以在此之后执行单击操作。

        3
  •  3
  •   Norayr Sargsyan    6 年前

    在新版本的Appium中,您可以使用:

    TouchActions action = new TouchActions(driver);
    action.scroll(element, 10, 100);
    action.perform();
    element.click();
    
        4
  •  1
  •   ShiyamTJ    7 年前
    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();

        5
  •  0
  •   alexander.polomodov Alex Hawking    8 年前

    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();
    
        6
  •  0
  •   Kiran Antony    6 年前

        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);
    }
    

    一旦在退出时检测到元素,就可以执行操作。

        7
  •  0
  •   AmitKS    5 年前

    String scrollElement = "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\"Progress Bar\").instance(0))";
    
    driver.findElementByAndroidUIAutomator(scrollElement).click();
    
        8
  •  0
  •   Ehsan Pagheh    4 年前

    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);
    

    推荐文章