Haza Kapo
Haza Kapo

Reputation: 11

Scrolling in Appium using Python

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'

Upvotes: 1

Views: 12553

Answers (2)

Pau Ballada
Pau Ballada

Reputation: 1618

For me worked using the swipe function:

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

Upvotes: 6

dmle
dmle

Reputation: 3658

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

Upvotes: 0

Related Questions