Appium + Python on Android - 滚动不起作用

Appium + Python on Android - scrolling doesn't work

Appium + Python Android - 滚动 我有一个应用程序可以自动执行测试。场景如下所示:

我点击日期选择器 > 出现日历

我点击年份 > 出现年份列表

我想滚动直到看到“1993”

屏幕上看不到“1993”这一年,我想继续滚动直到看到它。我试过了

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

^但我不想使用坐标,而且我不得不重复该行数次。

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

^但它一直向我抛出 selenium.common.exceptions.NoSuchElementException:消息:使用给定的搜索参数错误无法在页面上定位一个元素,因为正如我所说,它不可见

最好的方法是什么?

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

^这个给我一个错误,说一个元组没有 id

每次找不到元素时,Appium 都会抛出错误。 因此,当您定义变量可见性时,您的脚本会在滑动前停止。

试试这个:

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

您的脚本将向下滚动,直到找到 1993 年。