手机操作 设置认证 header AppiumConnection.extraheaders['XAccessToken'] sandbox.envdaccesstoken return webdriver.Remote( commandexecutorf" optionsoptions ) 等待 Appium 服务就绪 def waitforappium(): print("等待 Appium 服务就绪...") for i in range(30): try: resp requests.get( f" timeout5, ) if resp.statuscode 200: print(f"Appium 服务已就绪:{resp.statuscode}") return True else: print(f"Appium 服务未就绪...") except requests.exceptions.RequestException as e: print(f"Appium 服务未就绪...") time.sleep(2) print("Appium 服务启动超时") return False 等待Appium服务就绪 waitforappium() 创建 Appium driver driver createappiumdriver(sandbox) print(f"Appium 已连接,窗口大小: {driver.getwindowsize()}") print("n 按 HOME 键回到桌面...") driver.presskeycode(3) time.sleep(2) print("n 打开settings应用...") driver.activateapp("com.android.settings") time.sleep(2) print("n 向上滑动 5 秒...") 从屏幕下向上滑动 startx, starty 200, 200 endx, endy 200, 800 driver.swipe(startx, starty, endx, endy, duration5000) time.sleep(2) print("n 开始截图...") screenshotdir Path(file).parent / "screenshot" screenshotdir.mkdir(parentsTrue, existokTrue) timestamp time.strftime("%Y%m%d%H%M%S") screenshotpath screenshotdir / f"mobilescreenshot{timestamp}.png" driver.savescreenshot(str(screenshotpath)) print(f"n 截图已保存: {screenshotpath}") 关闭 driver.quit() sandbox.kill()