简体   繁体   中英

How to scroll using appium in 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("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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM