【问题标题】:Looking to ios and android scroll up and down till element found and then click寻找 ios 和 android 上下滚动直到找到元素,然后单击
【发布时间】:2020-07-04 22:21:09
【问题描述】:

我在 appium、java 中尝试了很多关于 ios 和 android 滚动的博客和讨论。但看起来大多数方法已被弃用或不起作用。请帮忙。 我正在使用 appium 1.15.1、java、selenium。

寻求快速帮助。

我正在尝试以下代码,但它滚动了两次,甚至没有点击。

嗨,

我在 Mac 上使用真实设备。 我正在使用带有 POM 的 Appium 1.15.1、Selenium、Java。 我为iOS向下滑动编写了以下代码。

嗨,

public void chkMenuOptioniOS1(String sName)
    {   
/* This is actual path which got from inspector
//XCUIElementTypeStaticText[@name="Account Information"]
*/
        String targetCell = "//XCUIElementTypeStaticText[contains(@name,'" + sName + "')]";
        MobileElement cellWithText = driver.findElement(By.xpath(targetCell));
        HashMap<String, String> scrollObject = new HashMap<String, String>();
        scrollObject.put("element", ((RemoteWebElement)cellWithText).getId());
        scrollObject.put("direction", "down");
        scrollObject.put("predicateString", "value == '" + sName + "'");
        scrollObject.put("toVisible", "true");
        driver.executeScript("mobile:scroll", scrollObject);
        try {
            System.out.println("In Try Block "+cellWithText.getText());
            Thread.sleep(10);
            cellWithText.click();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
  1. 它滚动了两次
  2. 它没有点击,

附加 DOM 对象屏幕截图。

请帮帮我

【问题讨论】:

    标签: java appium appium-android appium-ios


    【解决方案1】:

    这些是满足您对 iOS 和 Android 需求的方法:

    private JavascriptExecutor js;
    private HashMap<String, String> scrollObject = new HashMap<>();
    
    void iosScrollToAnElement(IOSElement el) {
            scrollObject.put("direction", "down");
            scrollObject.put("element", el.getId());
            js.executeScript("mobile: swipe", scrollObject);
    }
    
    void androidScrollToAnElementByText(String text) {
            try {
                ((AndroidDriver)driver).findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\"" + text + "\").instance(0))");
            } catch (Exception e) {
                throw new NoSuchElementException("No element" + e);
            }
        }
    

    在此之后,您需要在您正在寻找的特定元素上click()。 (只有滚动的方法)

    【讨论】:

    • 谢谢兄弟的回复。对于您使用 IOSElement 的 iOS,我正在使用 MobileElement,但它无法正常工作。对于Android,它第一次运行完美,当我调用相同的函数时,即androidScrollToAnElementByText,它一直在向上和向下滚动并且什么也不做。有什么想法吗?例如homePage.androidScrollToAnElementByText("Account Information") 获取帐户信息后点击它,然后调用下一个 androidScrollToAnElementByText("Log Out") 注销它会继续上下滚动几次并停止。
    • 除此之外,如果我提供不在页面上的文本,那么它会继续在 Android 上上下滚动。 iOS 运气不好
    • 对于 iOS 你可以使用IOSElement ir MobileElement (ios element extends mobile),它们都有getId() 方法。如果它一直滚动,则意味着您的页面中没有此类元素。对于 Android,text 只是一个示例。你可以传递任何你需要的东西来找到你的元素(可访问性 id、xpath 或其他)。因此,再次,如果它一直滚动,则意味着无法定位该元素
    • @Hari 这个方法的逻辑是:它会进行一次滚动迭代并尝试找到元素,如果没有这样的元素它会一直滚动直到它到达屏幕底部。如果他没有找到一个元素,它会启动相同的逻辑但向上滚动,如果没有元素它会停在屏幕顶部。
    • 你最好再问一个问题(但先进行一些调查)
    猜你喜欢
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 2021-05-22
    • 2021-09-12
    相关资源
    最近更新 更多