How to scroll in Appium using python


#19

Based on File "android_piQit.py", line 65, in testcategory_men it looks like you’re working with Android. Everything I’ve personally stated above is based my iOS testing experience thus far.

I’ve just begun working on Android testing, and just finally made it into views where the data exceeds the vertical screen space so I’m in the same boat as you right now. I’ll post details when I [hopefully] figure out some of the various options. Initially it seems like I’ll have to poll the raw page_source which will document ALL of the elements, including out-of-view elements, then leveraging that information to scroll. Or so I think… :slight_smile:

Scratch that, page_source fails to show all of the elements like I thought it would.


#20

Thanks, I will try that out too.
Will update here if I am able to get things ‘rolling’(literally) myself.


REQUEST: Android find_element and scroll tips
#21

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.


Display android List View contents in terminal using python and appium
#22

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?


#23

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.”


#24

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.


#25

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


#26

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


#27

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


#28

Appium 1.5

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


#29

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


#30

Python - iOS

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


#31

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)


#32

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


#33

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


#34

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…


#35

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


#36

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


#37

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.


#38

This worked like a charm. Thank you so much.