使用Python在Appium中滚动

huangapple go评论74阅读模式
英文:

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()

其中offsetXoffsetY表示偏移量。

如果您的应用程序具有可滚动的视图,您可以使用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(&lt;your locator to scroll from&gt;)
el2 = self.driver.find_element_by_accessibility_id(&#39;your locator to scroll to&#39;)
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(
            &#39;new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(&quot;Element text&quot;).instance(0));&#39;)

Of course, it is better to replace UiSelector().text(&quot;Element text&quot;) with uiselector().resourceid(elementId)

I suggest checking official functional tests in client repo as working examples

huangapple
  • 本文由 发表于 2020年1月3日 20:39:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定