main.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import subprocess
  2. import os
  3. import time
  4. def screenshot_basic(save_path="screenshot.png"):
  5. """
  6. 基础截图功能:先保存到手机,再拉取到电脑
  7. Args:
  8. save_path: 保存到电脑的路径
  9. """
  10. try:
  11. # 1. 在手机上进行截图并保存到/sdcard/目录
  12. print("正在截图...")
  13. subprocess.run(
  14. ["adb", "shell", "screencap", "-p", "/sdcard/temp_screen.png"],
  15. check=True, # check=True会在命令失败时抛出异常[citation:2]
  16. timeout=10
  17. )
  18. # 2. 将截图从手机拉取到电脑
  19. print("正在传输截图...")
  20. subprocess.run(
  21. ["adb", "pull", "/sdcard/temp_screen.png", save_path],
  22. check=True,
  23. timeout=10
  24. )
  25. # 3. 清理手机上的临时文件
  26. subprocess.run(
  27. ["adb", "shell", "rm", "/sdcard/temp_screen.png"],
  28. check=True,
  29. timeout=5
  30. )
  31. print(f"截图已保存到: {save_path}")
  32. return True
  33. except subprocess.CalledProcessError as e:
  34. print(f"ADB命令执行失败: {e}")
  35. return False
  36. except subprocess.TimeoutExpired as e:
  37. print(f"命令执行超时: {e}")
  38. return False
  39. except FileNotFoundError:
  40. print("未找到ADB命令,请确认ADB已安装并配置环境变量")
  41. return False
  42. def tap(x, y, device_serial=None):
  43. """
  44. 在指定坐标位置执行点击操作
  45. Args:
  46. x: X坐标
  47. y: Y坐标
  48. device_serial: 设备序列号(可选,多设备时需要指定)
  49. Returns:
  50. bool: 是否成功
  51. """
  52. try:
  53. cmd = ["adb"]
  54. if device_serial:
  55. cmd.extend(["-s", device_serial])
  56. cmd.extend(["shell", "input", "tap", str(x), str(y)])
  57. result = subprocess.run(cmd, capture_output=True, text=True, timeout=5)
  58. return result.returncode == 0
  59. except Exception as e:
  60. print(f"点击失败: {e}")
  61. return False
  62. def swipe_vertical(x, y1, y2, duration=300, device_serial=None):
  63. """
  64. 在指定X坐标位置,从y1垂直滑动到y2
  65. Args:
  66. x: X轴坐标
  67. y1: 起始Y坐标
  68. y2: 结束Y坐标
  69. duration: 滑动持续时间(毫秒),默认300ms
  70. device_serial: 设备序列号(可选,多设备时需要指定)
  71. Returns:
  72. bool: 是否成功
  73. """
  74. try:
  75. # 构建ADB命令
  76. cmd = ["adb"]
  77. if device_serial:
  78. cmd.extend(["-s", device_serial])
  79. cmd.extend([
  80. "shell", "input", "swipe",
  81. str(x), str(y1),
  82. str(x), str(y2),
  83. str(duration)
  84. ])
  85. print(f"滑动: ({x}, {y1}) -> ({x}, {y2}) 持续时间: {duration}ms")
  86. # 执行滑动
  87. result = subprocess.run(cmd, capture_output=True, text=True, timeout=5)
  88. if result.returncode == 0:
  89. print("滑动成功")
  90. return True
  91. else:
  92. print(f"滑动失败: {result.stderr}")
  93. return False
  94. except Exception as e:
  95. print(f"执行滑动时出错: {e}")
  96. return False
  97. # 使用示例
  98. if __name__ == "__main__":
  99. # 先确认设备已连接
  100. result = subprocess.run(["adb", "devices"], capture_output=True, text=True)
  101. print("当前连接的设备:")
  102. print(result.stdout)
  103. while True:
  104. swipe_vertical(100*2, 600*2, 300*2)
  105. time.sleep(0.5)
  106. swipe_vertical(100*2, 600*2, 300*2)
  107. time.sleep(0.5)
  108. swipe_vertical(100*2, 600*2, 300*2)
  109. time.sleep(0.5)
  110. swipe_vertical(100*2, 600*2, 300*2)
  111. time.sleep(0.5)
  112. swipe_vertical(100*2, 600*2, 300*2)
  113. time.sleep(0.5)
  114. swipe_vertical(100*2, 600*2, 300*2)
  115. time.sleep(0.5)
  116. tap(416 * 2, 790 * 2)
  117. time.sleep(1)
  118. tap(269 * 2, 875 * 2)
  119. time.sleep(2)
  120. # tap(103 * 4, 262 * 4)
  121. # time.sleep(num1)
  122. # swipe_vertical(138*4, 446*4, 578*4)
  123. # time.sleep(5)
  124. # time.sleep(2)
  125. screenshot_basic("my_screenshot.png")