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


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

    1.swipe(坐标到坐标)

    1. time.sleep(3)
    2. print("滑动开始")
    3. driver.swipe(500,1500,500,500)
    4. print("滑动结束")

    2. scroll(元素到元素)

    先定位元素组:

    //android.support.v7.widget.RecyclerView/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup

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

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

    1. # ########################## 定位数据区域,返回元素列表每次定位的有点多,所以使用字典去重 #########################################################
    2. try:
    3. # 使用XPath定位带有index属性的FrameLayout元素
    4. frame_layout_elements = WebDriverWait(driver, timeout, poll_frequency).until(
    5. EC.presence_of_all_elements_located(
    6. (By.XPATH, "//android.support.v7.widget.RecyclerView/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup"))
    7. )
    8. print("元素列表长度:", len(frame_layout_elements))
    9. for i in range(len(frame_layout_elements)):
    10. print("元素列表:",frame_layout_elements[i])
    11. print("准备元素滑动")
    12. time.sleep(5)
    13. driver.scroll(frame_layout_elements[-2], frame_layout_elements[1])
    14. # 根据需要进一步操作找到的唯一FrameLayout元素
    15. except Exception as e:
    16. print(f"无法定位到FrameLayout元素: {e}")

    3.drag_and_drop(元素,无素)

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

    1. # ########################## 定位数据区域,返回元素列表每次定位的有点多,所以使用字典去重 #########################################################
    2. try:
    3. # 使用XPath定位带有index属性的FrameLayout元素
    4. frame_layout_elements = WebDriverWait(driver, timeout, poll_frequency).until(
    5. EC.presence_of_all_elements_located(
    6. (By.XPATH, "//android.support.v7.widget.RecyclerView/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup"))
    7. )
    8. print("元素列表长度:", len(frame_layout_elements))
    9. for i in range(len(frame_layout_elements)):
    10. print("元素列表:",frame_layout_elements[i])
    11. print("准备元素滑动")
    12. time.sleep(5)
    13. driver.drag_and_drop(frame_layout_elements[-2], frame_layout_elements[1])
    14. # 根据需要进一步操作找到的唯一FrameLayout元素
    15. except Exception as e:
    16. print(f"无法定位到FrameLayout元素: {e}")

     

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

     

  • 相关阅读:
    IntelliJ IDEA 2023 v2023.2.5
    “魔盗”窃密木马(FakeCDR)研究
    安卓常见设计模式10------责任链模式(Kotlin版)
    最长上升子序列
    在Visual studio 2019中创建Qt+OpenCv项目
    vue - vuex实现持久化存储
    matlab 数据处理 命令集合
    脱离form表单校验input(校验单个input输入框)提交时边框变红
    Chrome开发工具与js加密
    Postgresql systemctl设置开机自启动
  • 原文地址:https://blog.csdn.net/weixin_42771529/article/details/139505640