How to scroll in Appium using python

For the native iOS folks using simulator testing, the following code will work (in Appium 1.3.4):

driver.execute_script("mobile: scrollTo", {"element": <webdriver object element>.id})

Just substitute ‘webdriver object element’ and it will scroll to the object.id. This is required because all gestures within iOS simulators are broken by Apple.

I’m attempting to scrollTo in a webview of a hybrid app and your suggested script doesn’t appear to work. At least on my iOS8 simulator.

self.driver.execute_script("mobile: scrollTo", {"element": <my_webdriver_element>.id})

I see the scrollbar activate in the webview, but it doesn’t physically scroll the page and the element does not come into view.

I’m not sure that scrollTo is supported in a webview context, I thought swipe methods would work in webviews though Have you tried TouchActions to scroll?

I’m using only native applications, so swipe gestures aren’t supported by Apple in the simulator. I can confirm that scrollTo is working in my iOS8 simulator within the native app.

In the native portions of the app all I have to do is call an element.click() and Appium happily scrollsTo the element automatically for me. Works great.

Still trying to figure out what works well in hybrid webview. Considering scrolling to elements is a pretty basic and repeatedly used function I’m surprised it’s not a provided function, or at least not easily solvable via a quick web search for the answer. But I’m really struggling to solve the challenge.

@SquareTheBase

I don’t know why but for Android 5.0 (Appium 1.3.4) this peace of code doesn’t work asserting next error on Appium Server:

[debug] Pushing command to appium work queue: ["element:scrollTo",{"text":null,"direction":"vertical","elementId":"18"}]
[debug] Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command.","origValue":"Could not scroll element into view: null"},"sessionId":"08768d88-603a-4bf9-bd1a-9f6191fbfa81"}

But this one works perfectly:

scrollObject = dict(direction="down", text="some_text", element=appium_driver_elem.id)
self.driver.execute_script("mobile: scrollTo", scrollObject)
1 Like

Note that I’ve only run Appium against the iOS simulators, so I cannot speak to Android. But I solved my Webview scrolling issues.

Basically while in the Native context, Appium will automatically scrollTo elements when you attempt to click on them. But you’ll still need to manually scrollTo them if you simply want to bring them into view for other reasons. Webview will function the same, however you need to specifically switch to the Webview context first (this was the missing key for me), and then remember to interact with the Webview elements using Selenium/HTML attributes. Then remember to switch back to the Native context before interacting with the native portions of your app.

You can read more details in this thread: Mobile: scrollTo fails in iOS hybrid webview

all the above option are not working for python

You need to provide much more detail for anyone to be able to offer assistance.

Pretend you’re filing a bug report - tell us about your AUT and environment, provide steps you’re attempting (your Python code), include app XML (self.driver.page_source or Cop XML from inspector), and any Appium logging for the failure.

Hi,
how to scroll to a particular content in python.

That was already covered above: driver.execute_script("mobile: scrollTo", {"element": <webdriver object element>.id})

could you plz tell how to find the element by id bcoz I am not able to find the element by id

one more question for hybrid apps I am not abe to find the web elements in UI Automator, for native apps, I could find the web elements.

You can get the webdriver element using whatever method you wish, then substitute that element for <webdriver object element> in the scrollTo code. Such as:

el = self.driver.find_element_by_xpath(<your_xpath>) driver.execute_script("mobile: scrollTo", {"element": el.id})

For Webview you need to switch to the Webview, then access the webview elements using Selenium code, then switch back to Nativeview before interacting with the native elements. Again, this is covered in posts above.

"You can read more details in this thread: Mobile: scrollTo fails in iOS hybrid webview "

I tried using the method you mentioned: try an operation on an element and appium handles the scroll to the element.
It doesnt work for my python client. If the elment is not in the field of view( which is why we need a scroll) it raises : NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

I also tried using the below but to no avail:
scrollobj=dict(direction=“down”, element=self.driver.find_element_by_name(“SUITS & BLAZERS”))
self.driver.execute_script(“mobile: scrollTo”,scrollobj)

Error:

ERROR: testcategory_men (main.TestHomeScreendisplay)

Traceback (most recent call last):
File “android_piQit.py”, line 65, in testcategory_men
scrollobj=dict(direction=“down”, element=self.driver.find_element_by_name(“SUITS & BLAZERS”))
File “/Library/Python/2.7/site-packages/selenium-2.46.0-py2.7.egg/selenium/webdriver/remote/webdriver.py”, line 324, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File “/Library/Python/2.7/site-packages/selenium-2.46.0-py2.7.egg/selenium/webdriver/remote/webdriver.py”, line 684, in find_element
{‘using’: by, ‘value’: value})[‘value’]
File “/Library/Python/2.7/site-packages/selenium-2.46.0-py2.7.egg/selenium/webdriver/remote/webdriver.py”, line 195, in execute
self.error_handler.check_response(response)
File “/Library/Python/2.7/site-packages/Appium_Python_Client-0.14-py2.7.egg/appium/webdriver/errorhandler.py”, line 29, in check_response
raise wde
NoSuchElementException: Message: An element could not be located on the page using the given search parameters.


Could you suggest aby other alternative. Like you said it is still a achallenge in appium for python clients. I have tried using many alternatives and while most dont work, the ones that do, do not have any effect on the native app.
I tried using the operation : driver.press(x=100,y=100).move_to(x=100,y=300).release().perform()
using valid cordinates and even though the code worked flawlessly,the screen did not show any signs of scroll.

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.

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

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.