test01.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # 坐标测试
  2. import uiautomator2 as u2
  3. import os
  4. command = "ls -l" # 你想要执行的 shell 命令
  5. return_code = os.system(command)
  6. print(f"命令 '{command}' 的返回码是: {return_code}")
  7. # 连接设备
  8. d = u2.connect()
  9. # 获取屏幕的尺寸
  10. width, height = d.window_size()
  11. print(f"屏幕尺寸: {width}x{height}")
  12. # 使用xpath定位按钮并获取其位置信息
  13. # button = d.xpath('//*[@content-desc="买入/开多"]').get()
  14. # if button:
  15. # # bounds返回(left, top, right, bottom)坐标
  16. # print(f"按钮坐标(left, top, right, bottom): {button.bounds}")
  17. # top_distance = button.bounds[1] # top坐标即是距离顶部的距离
  18. # print(f"按钮距离顶部: {top_distance}像素")
  19. # 屏幕向上滚动以查看底部内容 (从屏幕下方向上滑动)
  20. # fx, fy: 滑动的起始坐标点
  21. #
  22. #
  23. # fx: 起始点的 x 坐标
  24. # fy: 起始点的 y 坐标
  25. # tx, ty: 滑动的目标坐标点
  26. #
  27. #
  28. # tx: 目标点的 x 坐标
  29. # ty: 目标点的 y 坐标
  30. # duration: 滑动持续时间(以秒为单位)
  31. #
  32. # 可选参数,默认为 None
  33. # 用于控制滑动的速度
  34. # 如果同时设置了 steps 参数,duration 将被忽略
  35. # steps: 滑动的步数
  36. #
  37. #
  38. # 可选参数,默认为 None
  39. # 每一步大约需要 5 毫秒
  40. # 例如:设置 steps=200,整个滑动过程将持续约 1 秒(200 * 5ms = 1000ms)
  41. # 当设置了 steps 时,会忽略 duration 参数
  42. d.swipe(width // 2, height * 0.8, width // 2, height * 0.1)
  43. # buttons = d.xpath('//*[starts-with(@content-desc, "持仓")]').all()
  44. # button = d.xpath('//*[@content-desc="买入/开多"]').get()
  45. # button = d.xpath('//*[@content-desc="市价全平"]').get()
  46. # print(f"第一个 坐标(left, top, right, bottom): {button.bounds}")
  47. # buttons = d.xpath('//*[@content-desc="市价全平"]').all()
  48. #
  49. button = d.xpath('//*[@content-desc="手机"]').get()
  50. button.click()
  51. # for idx, button in enumerate(buttons):
  52. # print(f"按钮 {idx + 1} 坐标(left, top, right, bottom): {button.bounds}")
  53. # if button:
  54. # # bounds返回(left, top, right, bottom)坐标
  55. # print(f"按钮坐标(left, top, right, bottom): {button.bounds}")
  56. # top_distance = button.bounds[1] # top坐标即是距离顶部的距离
  57. # print(f"按钮距离顶部: {top_distance}像素")
  58. # d.swipe(width // 2, height * 0.8, width // 2, height * 0.3, duration=0.5)