手机自动化测试:5.模拟相关操作:swipe,scroll,drag_and_dropki

网上教程很多。以下仅参考。

1.swipe(坐标到坐标)

复制代码
time.sleep(3)
print("滑动开始")

driver.swipe(500,1500,500,500)
print("滑动结束")

2. scroll(元素到元素)

先定位元素组:

复制代码
//android.support.v7.widget.RecyclerView/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup

以下代码,获取元素列表成功。我们要利用这些个元素进行滚动的。

以下代码,滚动成功。注意的是:我使用的是-2和1,因为我用-1和0时老是滚动失败,原因以后再找吧。

复制代码
# ########################## 定位数据区域,返回元素列表每次定位的有点多,所以使用字典去重 #########################################################
try:
    # 使用XPath定位带有index属性的FrameLayout元素
    frame_layout_elements = WebDriverWait(driver, timeout, poll_frequency).until(
        EC.presence_of_all_elements_located(
            (By.XPATH, "//android.support.v7.widget.RecyclerView/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup"))
    )
    print("元素列表长度:", len(frame_layout_elements))

    for i in range(len(frame_layout_elements)):

        print("元素列表:",frame_layout_elements[i])

    print("准备元素滑动")
    time.sleep(5)
    driver.scroll(frame_layout_elements[-2], frame_layout_elements[1])


    # 根据需要进一步操作找到的唯一FrameLayout元素
except Exception as e:
    print(f"无法定位到FrameLayout元素: {e}")

3.drag_and_drop(元素,无素)

直接修改以上代码。以下测试通过。

复制代码
# ########################## 定位数据区域,返回元素列表每次定位的有点多,所以使用字典去重 #########################################################
try:
    # 使用XPath定位带有index属性的FrameLayout元素
    frame_layout_elements = WebDriverWait(driver, timeout, poll_frequency).until(
        EC.presence_of_all_elements_located(
            (By.XPATH, "//android.support.v7.widget.RecyclerView/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup"))
    )
    print("元素列表长度:", len(frame_layout_elements))

    for i in range(len(frame_layout_elements)):

        print("元素列表:",frame_layout_elements[i])

    print("准备元素滑动")
    time.sleep(5)
    driver.drag_and_drop(frame_layout_elements[-2], frame_layout_elements[1])


    # 根据需要进一步操作找到的唯一FrameLayout元素
except Exception as e:
    print(f"无法定位到FrameLayout元素: {e}")

下一节,对每一个元素块内的文字进行提取

相关推荐
咖啡の猫9 分钟前
Python字典推导式
开发语言·python
Swift社区23 分钟前
React Navigation 生命周期完整心智模型
前端·react.js·前端框架
若梦plus28 分钟前
从微信公众号&小程序的SDK剖析JSBridge
前端
leiming629 分钟前
C++ vector容器
开发语言·c++·算法
SystickInt1 小时前
C语言 strcpy和memcpy 异同/区别
c语言·开发语言
用泥种荷花1 小时前
Python环境安装
前端
CS Beginner1 小时前
【C语言】windows下编译mingw版本的glew库
c语言·开发语言·windows
Light601 小时前
性能提升 60%:前端性能优化终极指南
前端·性能优化·图片压缩·渲染优化·按需拆包·边缘缓存·ai 自动化
Jimmy1 小时前
年终总结 - 2025 故事集
前端·后端·程序员
烛阴1 小时前
C# 正则表达式(2):Regex 基础语法与常用 API 全解析
前端·正则表达式·c#