使用Python在Appium中滚动

2024-09-28 21:50:52 发布

您现在位置: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'

Tags: 代码fromcomactions屏幕driver方式element
1条回答
网友
1楼 · 发布于 2024-09-28 21:50:52

很可能是因为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")

我建议检查客户回购中的官方functional tests作为工作示例

相关问题 更多 >