繁体   English   中英

使用 Python 在 Appium 中滚动

[英]Scrolling in Appium using Python

我想向下滚动屏幕,但似乎无法正确操作。 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'

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

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

您很可能会收到错误消息,因为 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().resourceid(elementId)替换UiSelector().text("Element text") uiselector().resourceid(elementId)

我建议检查客户端存储库中的官方功能测试作为工作示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM