用下面的 adb 滑动方法试试
def adb_swipe_up(
start_x_percent=0.5,
start_y_percent=0.7,
end_x_percent=0.5,
end_y_percent=0.3,
duration=300,
max_attempts=3,
delay=1,
):
"""使用 ADB 命令执行上滑操作,支持自定义滑动参数和重试机制
该函数提供以下功能:
1. 支持通过百分比设置滑动起始和终点位置
2. 自动获取设备屏幕尺寸并计算实际坐标
3. 可配置滑动持续时间
4. 内置重试机制,处理滑动失败的情况
Args:
start_x_percent (float): 起始点 x 坐标在屏幕宽度上的百分比,默认 0.5(屏幕中间)
start_y_percent (float): 起始点 y 坐标在屏幕高度上的百分比,默认 0.7(屏幕偏下位置)
end_x_percent (float): 终点 x 坐标在屏幕宽度上的百分比,默认 0.5(屏幕中间)
end_y_percent (float): 终点 y 坐标在屏幕高度上的百分比,默认 0.3(屏幕偏上位置)
duration (int): 滑动持续时间(毫秒),默认 300ms
max_attempts (int): 最大重试次数,默认 3 次
delay (int): 重试间隔时间,默认 1 秒
Returns:
bool: 滑动操作是否成功。成功返回 True,失败返回 False
"""
for attempt in range(max_attempts):
try:
# 获取屏幕尺寸
output = subprocess.check_output(["adb", "shell", "wm", "size"])
size_str = output.decode("utf-8").strip()
width, height = map(int, size_str.split(": ")[1].split("x"))
# 计算实际滑动坐标
start_x = int(width * start_x_percent)
start_y = int(height * start_y_percent)
end_x = int(width * end_x_percent)
end_y = int(height * end_y_percent)
print(
f"开始执行滑动操作:从 ({start_x}, {start_y}) 滑动到 ({end_x}, {end_y})"
)
# 执行滑动操作
try:
subprocess.run(
[
"adb",
"shell",
"input",
"swipe",
str(start_x),
str(start_y),
str(end_x),
str(end_y),
str(duration),
],
check=True,
)
time.sleep(0.5) # 等待滑动动画完成
print("滑动操作执行成功")
return True
except subprocess.CalledProcessError as swipe_error:
print(f"滑动操作执行失败: {swipe_error}")
except Exception as e:
print(f"第 {attempt + 1} 次滑动尝试失败: {e}")
time.sleep(delay)
return False
你操作的是原生 app 还是小程序
安卓的 但是应该是 嵌套了 H5 页面 而且我打印出来的话 好像只有一个 NATIVE_APP 没办法切换的 老哥
后面用别的方法 确实能滚动了 但是现在有些 按钮定位后现实个 view 定位都定位不到 老哥知道怎么处理吗