【问题标题】:How to Scroll in Date Picker in Calendar using Appium Java?如何使用 Appium Java 在日历中滚动日期选择器?
【发布时间】:2021-12-17 14:27:53
【问题描述】:

我想在 Android 应用程序中选择过去/未来年份。我尝试了以下功能。但它不起作用。谁能帮帮我?

driver.findElementByAndroidUIAutomator("new UiScrollable(new     
UiSelector().scrollable(true).instance(0)).scrollIntoView(new     
UiSelector().textContains(\""+text+"\").instance(0))");

【问题讨论】:

    标签: java scroll appium appium-android


    【解决方案1】:

    我会用 Python 解释,你可以使用 Java 的等价物

    程序如下:

    1. 获取焦点元素属性以查找现在选择的内容。您可以检查顶部或年份选择器中心(绿色)元素上的年份。我已经在下图中指出了。

    current_focus_element = driver.find_element_by_xpath({#})  #xpath of the focused year or selected year text on the header
    
    focused_year_value = current_focus_element.get_attribute('value') # assume it is 2021
    
    1. 从Origin(当前关注的年份)向上或向下(逐年)滑动到Destination(当前年份上方或下方的年份):如果目标是过去(如2020年),我们应该向下滑动。为未来而奋斗。 (我们可以计算target year - current focused element。如果结果是positive,我们应该向下滑动。negative也向上滑动)
    # assuming the target is 2019
    
    difference = focused_year_value-target  # 2021-2019 = 2 (positive)
    
    # as it is positive we should swipe down. So we should find the coordinates of '2021' and '2022' and swipe from 2021 to 2022 for going down.
    
    origin_coordinates = driver.find_by_xpath(FOCUSED_YEAR_XPATH).location  #2021
    
    destination_coordinates = driver.find_by_xpath(BELOW_YEAR_XPATH).location  #2022
    
    driver.swipe(start_x=int(origin_coordinates['x']), start_y=int(origin_coordinates['y']),
    end_x=int(destination_coordinates['x']),end_y=int(destination_coordinates['y'])
    
    
    1. 检查焦点元素。如果我们到达目标,停止滑动,否则继续。
    focused_year_value = current_focus_element.get_attribute('value')
    
    if focusd_year_value == 2019:
        return
    
    else:
       # [Step 2]
    
    

    如果这有帮助,请点击绿色勾号。 :)

    【讨论】:

    • @Mohammand Monfared:我认为在 Appium 中,滑动功能已被弃用。所以需要检查替代解决方案。
    • @Madhukar 不,它没有。 i.imgur.com/QEVKDam.png
    猜你喜欢
    • 2018-04-07
    • 2019-09-28
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    相关资源
    最近更新 更多