Search code examples
pythonselenium-webdriverappium

Appium + Python on Android - scrolling doesn't work


Appium + Python on Android - scrolling I have an app to automate tests for. Scenario looks like this:

I tap on date picker > calendar appears

I tap on the year > a list of years appears

I want to scroll until '1993' is visible

the year '1993' is not visible on the screen and I want to keep scrolling until it is. I've tried

TouchAction(driver).press(x=746, y=1351).move_to(x=755, y=588).release().perform()

^but I don't want to use coordinates, plus I'd have to repeat that line several times.

def set_year(self):
visibility = self.driver.find_element(By.XPATH, "//android.widget.TextView[@text='1993']").is_displayed()
while not visibility:
TouchAction(self.driver).press(x=746, y=1351).move_to(x=755, y=588).release().perform()
visibility = self.driver.find_element(By.XPATH, "//android.widget.TextView[@text='1993']").is_displayed()
else:
print("not found")

^but it keeps throwing me selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters error, since as I said, it's not visible

What is the best approach for this?

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

^this one gives me an error saying that a tuple does not have id


Solution

  • Appium will throw an error each time an element is not find. So, your script stops before swiping, when you define your variable visibility.

    Try this :

    def set_year(self):
        visibility = False
        i = 0
    
        while not visibility or i<100:
            i += 1
            try:
                visibility = self.driver.find_element(By.XPATH, 
                    "//android.widget.TextView[@text='1993']").is_displayed()
            except:
                TouchAction(self.driver).press(x=746, y=1351).move_to(x=755, 
                                               y=588).release().perform()
        if not visibility:
            print("not found")
    

    Your script will scroll down until the year 1993 is found.