【问题标题】:Scrolling in Appium using Python使用 Python 在 Appium 中滚动
【发布时间】:2020-04-22 00:45:34
【问题描述】:

我想向下滚动屏幕,但似乎无法正确操作。 Appium的原方式如下:

actions = TouchAction(driver)
actions.scroll_from_element(element, 10, 100)
actions.scroll(10, 100)
actions.perform()

我的代码是:

actions = TouchAction(self.driver)
actions.scroll_from_element('com.android.dialer.dialers', x=90, y=1330)
actions.scroll(x=90, y=170)
actions.perform()

但是,我收到以下错误消息:

line 133, in test_app
scroll.scroll_from_element('com.android.dialer.dialers', x=90, y=1230)
AttributeError: 'TouchAction' object has no attribute 'scroll_from_element'

【问题讨论】:

    标签: python appium appium-android


    【解决方案1】:

    您很可能会收到该错误,因为 appium-python 客户端没有 scroll_from_element 函数。

    滚动屏幕的第一种方法是找到 2 个元素,然后从一个元素滚动到另一个元素。

    el1 = self.driver.find_element_by_accessibility_id(<your locator to scroll from>)
    el2 = self.driver.find_element_by_accessibility_id('your locator to scroll to')
    action = TouchAction(self.driver)
    action.press(el1).move_to(el2).release().perform()
    

    如果你想移动到有偏移的元素,你可以这样做:

    action.press(el1).move_to(el2, offsetX, offsetY).release().perform()
    

    其中 offsetX、offsetY 代表位移。

    如果您的应用具有可滚动视图,您可以使用 find+UIAutomator2 定位器滚动到所需元素:

    targetElement = self.driver.find_element_by_android_uiautomator(
                'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("Element text").instance(0));')
    

    当然,最好把UiSelector().text("Element text")换成uiselector().resourceid(elementId)

    我建议在客户端回购中查看官方functional tests 作为工作示例

    【讨论】:

      【解决方案2】:

      对我来说,使用滑动功能:

      #swipe(startX, startY, endX, endY, duration)
      self.driver.swipe(150, 400, 150, 200, 1000)
      

      【讨论】:

        猜你喜欢
        • 2014-12-26
        • 2021-02-28
        • 2016-12-16
        • 1970-01-01
        • 2016-05-26
        • 2019-09-25
        • 2018-01-01
        • 2015-11-17
        • 2017-08-27
        相关资源
        最近更新 更多