英文:
Scrolling in Appium using Python
问题
以下是翻译好的部分:
我想要滚动屏幕,但似乎无法做到正确。使用Appium的原始方法如下:
```python
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'对象没有'scroll_from_element'属性
英文:
I want to scroll down the screen, but I cannot seem to get it right. The original way with Appium is as follows:
actions = TouchAction(driver)
actions.scroll_from_element(element, 10, 100)
actions.scroll(10, 100)
actions.perform()
And my code is:
actions = TouchAction(self.driver)
actions.scroll_from_element('com.android.dialer.dialers', x=90, y=1330)
actions.scroll(x=90, y=170)
actions.perform()
However, I get the following error message:
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'
答案1
得分: 4
对我来说,使用滑动功能有效:
#滑动(startX, startY, endX, endY, 持续时间)
self.driver.swipe(150, 400, 150, 200, 1000)
英文:
For me worked using the swipe function:
#swipe(startX, startY, endX, endY, duration)
self.driver.swipe(150, 400, 150, 200, 1000)
答案2
得分: 0
您很可能会因为appium-python客户端没有scroll_from_element
函数而出现错误。
滚动屏幕的第一种方法是找到两个元素,然后从一个元素滚动到另一个元素。
el1 = self.driver.find_element_by_accessibility_id(<要滚动的定位器>)
el2 = self.driver.find_element_by_accessibility_id('要滚动到的定位器')
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("元素文本").instance(0));')
当然,最好将UiSelector().text("Element text")
替换为uiselector().resourceid(elementId)
。
我建议查看客户端存储库中的官方功能测试,这些是工作示例。
英文:
You are most likely getting the error because appium-python client has no scroll_from_element
function.
The first way to scroll the screen is to find 2 element, and scroll from one to another.
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()
If you want to move to element with offset, you can do it this way:
action.press(el1).move_to(el2, offsetX, offsetY).release().perform()
where offsetX, offsetY are representing the shift.
If your app has a scrollable view, you can scroll to required element using find+UIAutomator2 locator:
targetElement = self.driver.find_element_by_android_uiautomator(
'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("Element text").instance(0));')
Of course, it is better to replace UiSelector().text("Element text")
with uiselector().resourceid(elementId)
I suggest checking official functional tests in client repo as working examples
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论