6

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("Destination");

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('Starting')
el2 = self.driver.find_element_by_name('Ending')
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("DesinationText").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.

1
  • @Ganwk , your UI automator query should have any one of scrollview like listview , gridview or recyclerview. Please check this video which latets appium 1.5.3 version commands: youtube.com/watch?v=bT3tqaLNn-Y Commented Oct 11, 2016 at 5:12

3 Answers 3

5

self.driver.scroll(el1, el2) is not working for me either.

I started using

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

driver.swipe(start_x, start_y, end_x, end_y, duration) will be available in _touch.py file

Swipe down slowly, add a logic to check if the element is present - continue till the element is found.

Sign up to request clarification or add additional context in comments.

Comments

4

I ended up using coordinate even though its not the best solution it works for me at the moment

action = TouchAction(self.driver)
el = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.NAME, loc)))
action.press(el).move_to(x=10, y=-500).release().perform()

1 Comment

is the negative value working for you?Coz when give a negative y axis i get The coordinates provided to an interactions operation are invalid.
-1

instance(i) is like index(i) but for some reason works better.

what is it used for? let's say that in the current activity we are testing (which is present on the screen), there are 10 TextView views. in that case the following selector:

new UiSelector().className("android.widget.TextView")

is a good match for all of the 10 TextView views we have on the screen, but if you don't specify exactly which instance\index you want to match, it will return the first one by default. But, let's assume you want to have a match specifically for the 10th TextView, just use the following selector:

new UiSelector().className("android.widget.TextView").instance(10)

The code you have written is good and is working. i have wrapped it up with example of how to handle errors:

def scroll_to_text(text):
try:
    element = driver.find_element_by_android_uiautomator(
        'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("'+text+'").instance(0));')
    return element
except NoSuchElementException:
    logger.error('it was not found')
    return None
except Exception, e:
    logger.error("Unexpected error:")
    logger.exception(e)
    return None

So I guess you have a different problem than what you thought originally. Please note:

  1. new UiSelector().text("DesinationText") will try to match an element that will have the exact text "DesinationText". if it is contained in a larger text it won't be matched.
  2. 'new UiScrollable(new UiSelector().scrollable(true).instance(0)' in your code assumes that there is only one scrollable element in the current screen. maybe you have two listviews and the text is in another listview or any other scrollable item?

Hope this helps. Gil.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.