【问题标题】:How to scroll down to click the element in Android using appium and java?如何使用appium和java向下滚动以单击Android中的元素?
【发布时间】:2018-03-14 23:59:03
【问题描述】:

我想知道如何使用appium和java在Android中向下滚动点击元素?

我在“android.support.v7.widget.RecyclerView”中有一个元素列表。由于它有10多个元素,我们需要滑动屏幕才能看到下面的元素。 每个元素都有相同的 id,即“com.osanda.exampleapp/textViewTitle”。但是它们的文字不同,比如“Apple”、“Orange”、“Grapes”......

我只需要使用其文本滚动并单击相关元素(“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());
        }

谢谢。

【问题讨论】:

    标签: java android scroll automation appium


    【解决方案1】:

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

    public void scrollAndClick(String visibleText) {
         androidDriver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+visibleText+"\").instance(0))").click();
            }
        }
    

    【讨论】:

    • 只是好奇,这种技术是否也可以与 xpath 一起使用。例如,如果我们用定位器替换 textContains。
    • @KovidMehta 我认为您可以使用以下通用函数进行滚动。 public void scrollUsingTouchActionsByElements(MobileElement startElement, MobileElement endElement) { TouchAction actions = new TouchAction(driver); actions.press(startElement).waitAction(Duration.ofSeconds(2)).moveTo(endElement).release().perform(); } 如果这对你有用,请告诉我。
    • 在安卓应用中搜索元素之前刷新页面。
    • 此方法向下滚动表单以查找搜索文本。有没有其他方法可以从上到下
    • ddjadav 你找到解决办法了吗?
    【解决方案2】:

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

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

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

    【讨论】:

    • 我为我的自动化脚本尝试了您的解决方案。它所做的是向上滚动一点然后向下滚动。这样它会滚动 2-3 次,但没有到达要显示的预期元素。我的应用程序甚至可以有 50-60 个条目,并且在某个时间点屏幕上的条目不能超过 8-9 个。因此,如果上述脚本必须工作,您认为需要更改的内容。谢谢。
    【解决方案3】:

    在新版本的 Appium 中你可以使用这个:

    TouchActions action = new TouchActions(driver);
    action.scroll(element, 10, 100);
    action.perform();
    element.click();
    

    【讨论】:

    • 对我不起作用。不得不使用 driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text(\"myText"));").click();
    【解决方案4】:
    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();

    【讨论】:

    • 这段代码中的point 是什么?此外,“findElement(java.lang.String, java.lang.String)”在“org.openqa.selenium.remote.RemoteWebDriver”中具有受保护的访问权限
    【解决方案5】:

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

    【讨论】:

    • Lokesh,我试过你的代码,但它只滚动屏幕 1-2 厘米一次,最终未能达到预期的文本/元素。
    【解决方案6】:

    使用下面的 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);
    }
    

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

    【讨论】:

      【解决方案7】:

      这对我有用,当文本可见时滚动将停止。这里我的文字是:“进度条”,只需根据您的文字替换。它会起作用的。

      String scrollElement = "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\"Progress Bar\").instance(0))";
      
      driver.findElementByAndroidUIAutomator(scrollElement).click();
      

      【讨论】:

        【解决方案8】:

        你可以用这个

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

        它将滚动直到文本可见。

        【讨论】:

          猜你喜欢
          • 2017-03-10
          • 2017-08-31
          • 1970-01-01
          • 2019-12-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-17
          • 1970-01-01
          相关资源
          最近更新 更多