APP自动化-多页列表滑动获取元素

在APP自动化过程中,遇到目录类的元素需要获取元素里的值,如果存在多页,需要滑动很多次,可以通过循环+源码变化来判断是否滑动到底

# wait.until(ec.visibility_of_element_located(locator)).click()   #点击目录
# 开始正序list
locator = (By.ID,"com.zhao.myreader:id/tv_chapter_title")
elements = wait.until(ec.visibility_of_all_elements_located(locator))   #获取目录元素组
# 第一种写法
# list1 = []
# for i in elements:
#     list1.append(i.text)

# 第二种写法
list1 = [i.text for i in elements]  # 列表推导式的写法

locator = (By.ID, "com.zhao.myreader:id/lv_chapter_list")
page_ele = wait.until(ec.visibility_of_element_located(locator))    # 获取目录列表元素块

print(page_ele.location)    # 返回位置信息    {'x': 0, 'y': 201}  X是水平的宽,y是垂直的坐标
print(page_ele.size)        # 返回元素的尺寸   {'height': 1719, 'width': 888}
# 得出滑动的两个位置信息
x = page_ele.location["x"] + 0.5 * page_ele.size["width"]   # x坐标+元素尺寸二分之一的宽度位置

y1 = page_ele.location["y"] + 0.3 * page_ele.size["height"] # Y坐标+元素尺寸三分之一的高度位置
y2 = page_ele.location["y"] + 0.6 * page_ele.size["height"] # Y坐标+元素尺寸三分之二的高度位置

# 只有两页数量少的做法
# action = TouchAction(driver)
# action.press(x=x, y=y2).move_to(x=x, y=y1).release().perform()

# 页码多的做法
while True:
    sourse = driver.page_source # 获取页面源码
    locator = (By.ID, "com.zhao.myreader:id/tv_chapter_title")
    elements = wait.until(ec.visibility_of_all_elements_located(locator))
    elements_text = [i.text for i in elements]

    # for i in elements_text: # 去重
    #     if i not in list1:
    #         list1.append(i)
    [list1.append(i) for i in elements_text if i not in list1]

    action = TouchAction(driver)
    action.press(x=x, y=y2).move_to(x=x, y=y1).release().perform()

    if sourse == driver.page_source:  # 当滑动后页面源码和滑动前的页面源码相同时,表示滑动到底,结束循环
        break
print(list1)
print(len(list1))

 

posted @ 2023-05-11 11:02  琉璃星眸  阅读(44)  评论(0编辑  收藏  举报