test01.py 2.0 KB

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