How to scroll in Appium using python

Here’s a successful method to scroll in Android.

element_to_tap = self.driver.find_element_by_xpath(<xpath_to_element_near_bottom_of_screen>) element_to_drag_to = self.driver.find_element_by_xpath(<xpath_to_element_near_top_of_screen>) self.driver.scroll(element_to_tap, element_to_drag_to)

All you need to do is feed it two webdriverobjects, it seems to do the rest. But I haven’t fully validated it works in all of my scenarios, that it can scroll down as well as up, that it doesn’t over-scroll, etc.

It doesn’t look like Android will ever provide page_source that contains out-of-view elements, including after you scroll (items at the top of the page are now out-of-view after a scroll and are dropped from XML). So there’s going to have to be some pretty clever functions coded to support all of the various scrolling I will need to do.

For example, many of my tests need to count the number of rows //android.widget.ListView/android.widget.LinearLayout in my view to make sure it matches summary counts elsewhere in the app. I’m going to need to scroll, count up the visible rows, scroll more, count up the visible rows making sure not to count ones already counted (guess I’ll have to keep track of some hopefully visible unique identifier in each row), and stop counting once the list has beens scrolled (again keeping track of hopefully visible unique identifier and stop scrolling once no new uniques appear after a scroll). It all seems overly complicated. I’d think by now these would be basic functions provided by Appium.

I’m hoping someone with more experience will chime in with the “best” way to do this.

4 Likes

Hey Christopher,

Thanks for the details. Not sure to understand why we cannot do that for iOS part.

I also found scrollTo for iOS, but it seems to be used to scroll in a UITableView to a content, not to a defined cell / web element.

Do you have an other way to do that than avoiding a script?

scrollTo is only implemented in the iOS native context. I haven’t personally attempted scrolling in iOS WebView at this time.

You can try: "mobile: scroll" in the Webview. I don’t have more specific details, unfortunately.

According this: https://github.com/appium/appium/issues/3974 the answer is: “…the correct way to achieve what you want is to use mobile: scroll and then check that the element in question is within the bounds of the viewport.”

Okay thanks, however, I wasn’t be able to make scrollTo work for native context in iOS.

According to this one, Scroll To / Swipe Action in iOS 8 it’s not working anyway.

@Christopher_Graham thanks a lot for the solution.
Guys this worked perfectly for me in Android Native app - Real device testing.

Did anyone find a way to scroll down in a Hybrid App Ios and Android? Please help me with this.

Im getting this error"

Traceback (most recent call last):
File “appium_test.py”, line 27, in
action.press(el).move_to(x=10, y=-500).release().perform()
File “C:\Python27\lib\site-packages\appium\webdriver\common\touch_action.py”, line 94, in perform
self._driver.execute(Command.TOUCH_ACTION, params)
File “C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py”, line 196, in execute
self.error_handler.check_response(response)
File “C:\Python27\lib\site-packages\appium\webdriver\errorhandler.py”, line 24, in check_response
super(MobileErrorHandler, self).check_response(response)
File “C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py”, line 103, in check_response
status = value[‘status’]
TypeError: string indices must be integers

Appium 1.5

el = self.driver.find_element_by_xpath(“xpath”)
self.driver.execute_script(“mobile: scroll”, {“direction”: ‘down’, ‘element’: el})

I have tested this on apk: APIDemo.apk

def test_swipe(self):
x = []
sleep(1)
self.driver.find_element_by_name(“Views”).click()
sleep(1)
while x == []: #Perform below till i get the desired item which i am looking for. In this case the item is in second page
x = self.driver.find_elements_by_name(“Grid”)
if x == []: #Perform the scroll only if not able to locate the desired item in current screen
size = self.driver.get_window_size()
starty = int(size[“height”] * 0.80)
endy = int(size[“height”]*0.20)
startx = int(size[“width”]/2)
sleep(2)
self.driver.swipe(startx, starty, startx, endy, 3000) #Swipe from bottom to top
sleep(2)
else:
pass
sleep(1)
x[0].click()

Python - iOS

el = self.driver.find_element_by_id(‘Logout’)
self.driver.execute_script(‘mobile: scroll’, {“element”: el, “toVisible”: True})

Python - Android.

Change the value of end_y depending on how much you try to scroll down for each swipe.

self.driver.swipe(470, 1400, 470, 1200, 400)

driver.swipe(start_x, start_y, end_x, end_y, duration)

1 Like

Doesn’t work for me.

‘appium-version’: ‘1.6.3’,
‘platformName’: ‘iOS’,
‘platformVersion’: ‘10.0.2’,

I try:

self.driver.scroll(el1, el2)

el = self.driver.find_element_by_xpath(‘xpath’)
self.driver.execute_script(‘mobile: scroll’, {“element”: el, “toVisible”: True})

touch = TouchAction(self.driver)
touch.press(el1).wait(ms).move_to(el2).release().perform()

in the last case, I have got an error

Remove these lines from your code:
self.driver.scroll(el1, el2)
touch = TouchAction(self.driver)
touch.press(el1).wait(ms).move_to(el2).release().perform()

And just try using the below for iOS app:

el = self.driver.find_element_by_xpath('xpath_value')
self.driver.execute_script('mobile: scroll', {"element": el, "toVisible": True})

If you still see error message can you share your logs here. Try if you can use id instead of xpath

Yes, if i use id, it’s work fine.
But if i use xpath, it’s just executed and nothing happens
Appium thinks that element isVisible

[debug] [MJSONWP] Calling AppiumDriver.execute() with args: [“mobile: scroll”,[{“toVisible”:true,“element”:{“element-6066-11e4-a52e-4f735466cecf”:“A0419246-16D7-453D-B11C-701BCC82D063”,“ELEMENT”:“A0419246-16D7-453D-B11C-701BCC82D063”}}],“0dad8e66-3ee1-4ab6-9793-40d59a0feb21”]
[debug] [XCUITest] Executing command ‘execute’
[debug] [JSONWP Proxy] Proxying [POST /uiaElement/A0419246-16D7-453D-B11C-701BCC82D063/scroll] to [POST http://localhost:8100/session/6BB040E5-A3C7-4195-95A5-7399116145F3/uiaElement/A0419246-16D7-453D-B11C-701BCC82D063/scroll] with body: {“toVisible”:true}
[debug] [JSONWP Proxy] Got response with status 200: {“value”:{},“sessionId”:“6BB040E5-A3C7-4195-95A5-7399116145F3”,“status”:0}
[debug] [MJSONWP] Responding to client with driver.execute() result: null

Unfortunately, I have few places, where i can have some elements with identical ID’s…

Ok, if I change my xpath and looking not the cell in table, and looking some elements in this cell, scroll works well.
I have no idei why it’s happens

Glad that it worked for id & xpath. Yup, xpath will be weird at times

After a lot of searching, this worked for me. Thanks.

Can you also tell how to scroll using coordinates, I tried with TouchAction(long_press and move_to) but it did not worked.

This worked like a charm. Thank you so much.

Thanks. The same worked for me too.

el1 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.support.v7.widget.RecyclerView/android.widget.LinearLayout[4]/android.widget.RelativeLayout")

el2 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.support.v7.widget.RecyclerView/android.widget.LinearLayout[6]/android.widget.RelativeLayout")

driver.scroll(el2, el1)

Able to scroll in Android Native with:

self.driver.find_element_by_android_uiautomator("new UiScrollable(new UiSelector()).scrollIntoView(text(\"EnterText\"))")

Just add the ‘text’ value and try.

1 Like