How to scroll in Appium using python

Hi I want to know how to scroll in appium using python

I have done it in Java before and it worked really well.
driver.scrollTo(“Text”);

However I can’t seem to do the same in python. I am fairly new to python and I have try doing multiple ways to scroll

One way I tried was
el1 = self.driver.find_element_by_name(‘Text1’)
el2 = self.driver.find_element_by_name(‘Text2’)
self.driver.scroll(el1, el2)

second way I tried was
self.driver.find_element_by_android_uiautomator(‘new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(“Some Text”).instance(0));’).

second way scroll once but didn’t scroll all the way down so it threw NoSuchElementException.
Also, can someone explain to me what instances are in UiSelector?
I read the description but I didn’t quite understand
Instance: Set the search criteria to match the widget by its instance number.

I was going to ask the same question. Please Someone answer.

I use move_to, like this:
action = TouchAction(driver)
action.press(el1).move_to(el2).release().perform()

2 Likes

@116 Are you using an iOS native app? If so, does the above work for you in the simulator?

I am using an Android native app, I am not using the simulator.

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.