mobile-mcp-ai 2.5.9__py3-none-any.whl → 2.5.10__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -31,6 +31,9 @@ class BasicMobileToolsLite:
31
31
 
32
32
  # 操作历史(用于生成 pytest 脚本)
33
33
  self.operation_history: List[Dict] = []
34
+
35
+ # 目标应用包名(用于监测应用跳转)
36
+ self.target_package: Optional[str] = None
34
37
 
35
38
  def _is_ios(self) -> bool:
36
39
  """判断当前是否为 iOS 平台"""
@@ -53,34 +56,157 @@ class BasicMobileToolsLite:
53
56
  }
54
57
  self.operation_history.append(record)
55
58
 
56
- def _get_full_hierarchy(self) -> str:
57
- """获取完整的 UI 层级 XML(包含 NAF 元素)
59
+ def _get_current_package(self) -> Optional[str]:
60
+ """获取当前前台应用的包名/Bundle ID"""
61
+ try:
62
+ if self._is_ios():
63
+ ios_client = self._get_ios_client()
64
+ if ios_client and hasattr(ios_client, 'wda'):
65
+ app_info = ios_client.wda.session().app_current()
66
+ return app_info.get('bundleId')
67
+ else:
68
+ info = self.client.u2.app_current()
69
+ return info.get('package')
70
+ except Exception:
71
+ return None
72
+
73
+ def _check_app_switched(self) -> Dict:
74
+ """检查是否已跳出目标应用
58
75
 
59
- 优先使用 ADB 直接 dump,比 uiautomator2.dump_hierarchy 更完整
76
+ Returns:
77
+ {
78
+ 'switched': bool, # 是否跳转
79
+ 'current_package': str, # 当前应用包名
80
+ 'target_package': str, # 目标应用包名
81
+ 'message': str # 提示信息
82
+ }
60
83
  """
61
- import sys
84
+ if not self.target_package:
85
+ return {
86
+ 'switched': False,
87
+ 'current_package': None,
88
+ 'target_package': None,
89
+ 'message': '⚠️ 未设置目标应用,无法监测应用跳转'
90
+ }
62
91
 
63
- if self._is_ios():
64
- # iOS 使用 page_source
65
- ios_client = self._get_ios_client()
66
- if ios_client and hasattr(ios_client, 'wda'):
67
- return ios_client.wda.source()
68
- return ""
92
+ current = self._get_current_package()
93
+ if not current:
94
+ return {
95
+ 'switched': False,
96
+ 'current_package': None,
97
+ 'target_package': self.target_package,
98
+ 'message': '⚠️ 无法获取当前应用包名'
99
+ }
100
+
101
+ if current != self.target_package:
102
+ return {
103
+ 'switched': True,
104
+ 'current_package': current,
105
+ 'target_package': self.target_package,
106
+ 'message': f'⚠️ 应用已跳转!当前应用: {current},目标应用: {self.target_package}'
107
+ }
108
+
109
+ return {
110
+ 'switched': False,
111
+ 'current_package': current,
112
+ 'target_package': self.target_package,
113
+ 'message': f'✅ 仍在目标应用: {current}'
114
+ }
115
+
116
+ def _return_to_target_app(self) -> Dict:
117
+ """返回到目标应用
118
+
119
+ 策略:
120
+ 1. 先按返回键(可能关闭弹窗或返回上一页)
121
+ 2. 如果还在其他应用,启动目标应用
122
+ 3. 验证是否成功返回
123
+
124
+ Returns:
125
+ {
126
+ 'success': bool,
127
+ 'message': str,
128
+ 'method': str # 使用的返回方法
129
+ }
130
+ """
131
+ if not self.target_package:
132
+ return {
133
+ 'success': False,
134
+ 'message': '❌ 未设置目标应用,无法返回',
135
+ 'method': None
136
+ }
69
137
 
70
- # Android: 优先使用 ADB 直接 dump
71
138
  try:
72
- # 方法1: ADB dump(获取最完整的 UI 树,包括 NAF 元素)
73
- self.client.u2.shell('uiautomator dump /sdcard/ui_dump.xml')
74
- result = self.client.u2.shell('cat /sdcard/ui_dump.xml')
75
- if result and isinstance(result, str) and result.strip().startswith('<?xml'):
76
- xml_string = result.strip()
77
- self.client.u2.shell('rm /sdcard/ui_dump.xml')
78
- return xml_string
139
+ # 先检查当前应用
140
+ current = self._get_current_package()
141
+ if not current:
142
+ return {
143
+ 'success': False,
144
+ 'message': '❌ 无法获取当前应用包名',
145
+ 'method': None
146
+ }
147
+
148
+ # 如果已经在目标应用,不需要返回
149
+ if current == self.target_package:
150
+ return {
151
+ 'success': True,
152
+ 'message': f'✅ 已在目标应用: {self.target_package}',
153
+ 'method': 'already_in_target'
154
+ }
155
+
156
+ # 策略1: 先按返回键(可能关闭弹窗或返回)
157
+ if self._is_ios():
158
+ ios_client = self._get_ios_client()
159
+ if ios_client and hasattr(ios_client, 'wda'):
160
+ # iOS 返回键
161
+ ios_client.wda.press('home') # iOS 先按 home
162
+ time.sleep(0.5)
163
+ # 然后启动目标应用
164
+ ios_client.wda.app_activate(self.target_package)
165
+ else:
166
+ return {
167
+ 'success': False,
168
+ 'message': '❌ iOS 客户端未初始化',
169
+ 'method': None
170
+ }
171
+ else:
172
+ # Android: 先按返回键
173
+ self.client.u2.press('back')
174
+ time.sleep(0.5)
175
+
176
+ # 检查是否已返回
177
+ current = self._get_current_package()
178
+ if current == self.target_package:
179
+ return {
180
+ 'success': True,
181
+ 'message': f'✅ 已返回目标应用: {self.target_package}(通过返回键)',
182
+ 'method': 'back_key'
183
+ }
184
+
185
+ # 如果还在其他应用,启动目标应用
186
+ self.client.u2.app_start(self.target_package)
187
+ time.sleep(1)
188
+
189
+ # 验证是否成功返回
190
+ current = self._get_current_package()
191
+ if current == self.target_package:
192
+ return {
193
+ 'success': True,
194
+ 'message': f'✅ 已返回目标应用: {self.target_package}',
195
+ 'method': 'app_start'
196
+ }
197
+ else:
198
+ return {
199
+ 'success': False,
200
+ 'message': f'❌ 返回失败:当前应用仍为 {current},期望 {self.target_package}',
201
+ 'method': 'app_start'
202
+ }
79
203
  except Exception as e:
80
- print(f" ⚠️ ADB dump 失败: {e}", file=sys.stderr)
81
-
82
- # 方法2: 回退到 uiautomator2
83
- return self.client.u2.dump_hierarchy(compressed=False)
204
+ return {
205
+ 'success': False,
206
+ 'message': f'❌ 返回目标应用失败: {e}',
207
+ 'method': None
208
+ }
209
+
84
210
 
85
211
  # ==================== 截图 ====================
86
212
 
@@ -381,7 +507,7 @@ class BasicMobileToolsLite:
381
507
  if show_popup_hints and not self._is_ios():
382
508
  try:
383
509
  import xml.etree.ElementTree as ET
384
- xml_string = self._get_full_hierarchy()
510
+ xml_string = self.client.u2.dump_hierarchy(compressed=False)
385
511
  root = ET.fromstring(xml_string)
386
512
 
387
513
  # 检测弹窗区域
@@ -558,7 +684,7 @@ class BasicMobileToolsLite:
558
684
  else:
559
685
  try:
560
686
  import xml.etree.ElementTree as ET
561
- xml_string = self._get_full_hierarchy()
687
+ xml_string = self.client.u2.dump_hierarchy(compressed=False)
562
688
  root = ET.fromstring(xml_string)
563
689
 
564
690
  for elem in root.iter():
@@ -963,25 +1089,41 @@ class BasicMobileToolsLite:
963
1089
  ref=f"coords_{x}_{y}"
964
1090
  )
965
1091
 
1092
+ # 🎯 关键步骤:检查应用是否跳转,如果跳转则自动返回目标应用
1093
+ app_check = self._check_app_switched()
1094
+ return_result = None
1095
+
1096
+ if app_check['switched']:
1097
+ # 应用已跳转,尝试返回目标应用
1098
+ return_result = self._return_to_target_app()
1099
+
1100
+ # 构建返回消息
966
1101
  if converted:
967
1102
  if conversion_type == "crop_offset":
968
- return {
969
- "success": True,
970
- "message": f"✅ 点击成功: ({x}, {y})\n"
971
- f" 🔍 局部截图坐标转换: ({original_x},{original_y}) + 偏移({crop_offset_x},{crop_offset_y}) → ({x},{y})"
972
- }
1103
+ msg = f"✅ 点击成功: ({x}, {y})\n" \
1104
+ f" 🔍 局部截图坐标转换: ({original_x},{original_y}) + 偏移({crop_offset_x},{crop_offset_y}) → ({x},{y})"
973
1105
  else:
974
- return {
975
- "success": True,
976
- "message": f" 点击成功: ({x}, {y})\n"
977
- f" 📐 坐标已转换: ({original_x},{original_y}) → ({x},{y})\n"
978
- f" 🖼️ 图片尺寸: {image_width}x{image_height} → 屏幕: {screen_width}x{screen_height}"
979
- }
1106
+ msg = f"✅ 点击成功: ({x}, {y})\n" \
1107
+ f" 📐 坐标已转换: ({original_x},{original_y}) → ({x},{y})\n" \
1108
+ f" 🖼️ 图片尺寸: {image_width}x{image_height} → 屏幕: {screen_width}x{screen_height}"
980
1109
  else:
981
- return {
982
- "success": True,
983
- "message": f"✅ 点击成功: ({x}, {y}) [相对位置: {x_percent}%, {y_percent}%]"
984
- }
1110
+ msg = f"✅ 点击成功: ({x}, {y}) [相对位置: {x_percent}%, {y_percent}%]"
1111
+
1112
+ # 如果检测到应用跳转,添加警告和返回结果
1113
+ if app_check['switched']:
1114
+ msg += f"\n{app_check['message']}"
1115
+ if return_result:
1116
+ if return_result['success']:
1117
+ msg += f"\n{return_result['message']}"
1118
+ else:
1119
+ msg += f"\n❌ 自动返回失败: {return_result['message']}"
1120
+
1121
+ return {
1122
+ "success": True,
1123
+ "message": msg,
1124
+ "app_check": app_check,
1125
+ "return_to_app": return_result
1126
+ }
985
1127
  except Exception as e:
986
1128
  return {"success": False, "message": f"❌ 点击失败: {e}"}
987
1129
 
@@ -1114,9 +1256,9 @@ class BasicMobileToolsLite:
1114
1256
  return {"success": False, "message": f"❌ 点击失败: {e}"}
1115
1257
 
1116
1258
  def _find_element_in_tree(self, text: str) -> Optional[Dict]:
1117
- """在 XML 树中查找包含指定文本的元素(使用完整 UI 层级)"""
1259
+ """在 XML 树中查找包含指定文本的元素"""
1118
1260
  try:
1119
- xml = self._get_full_hierarchy()
1261
+ xml = self.client.u2.dump_hierarchy(compressed=False)
1120
1262
  import xml.etree.ElementTree as ET
1121
1263
  root = ET.fromstring(xml)
1122
1264
 
@@ -1510,7 +1652,28 @@ class BasicMobileToolsLite:
1510
1652
  elem.set_text(text)
1511
1653
  time.sleep(0.3)
1512
1654
  self._record_operation('input', element=resource_id, ref=resource_id, text=text)
1513
- return {"success": True, "message": f"✅ 输入成功: '{text}'"}
1655
+
1656
+ # 🎯 关键步骤:检查应用是否跳转,如果跳转则自动返回目标应用
1657
+ app_check = self._check_app_switched()
1658
+ return_result = None
1659
+ if app_check['switched']:
1660
+ return_result = self._return_to_target_app()
1661
+
1662
+ msg = f"✅ 输入成功: '{text}'"
1663
+ if app_check['switched']:
1664
+ msg += f"\n{app_check['message']}"
1665
+ if return_result:
1666
+ if return_result['success']:
1667
+ msg += f"\n{return_result['message']}"
1668
+ else:
1669
+ msg += f"\n❌ 自动返回失败: {return_result['message']}"
1670
+
1671
+ return {
1672
+ "success": True,
1673
+ "message": msg,
1674
+ "app_check": app_check,
1675
+ "return_to_app": return_result
1676
+ }
1514
1677
  return {"success": False, "message": f"❌ 输入框不存在: {resource_id}"}
1515
1678
  else:
1516
1679
  elements = self.client.u2(resourceId=resource_id)
@@ -1524,7 +1687,28 @@ class BasicMobileToolsLite:
1524
1687
  elements.set_text(text)
1525
1688
  time.sleep(0.3)
1526
1689
  self._record_operation('input', element=resource_id, ref=resource_id, text=text)
1527
- return {"success": True, "message": f"✅ 输入成功: '{text}'"}
1690
+
1691
+ # 🎯 关键步骤:检查应用是否跳转,如果跳转则自动返回目标应用
1692
+ app_check = self._check_app_switched()
1693
+ return_result = None
1694
+ if app_check['switched']:
1695
+ return_result = self._return_to_target_app()
1696
+
1697
+ msg = f"✅ 输入成功: '{text}'"
1698
+ if app_check['switched']:
1699
+ msg += f"\n{app_check['message']}"
1700
+ if return_result:
1701
+ if return_result['success']:
1702
+ msg += f"\n{return_result['message']}"
1703
+ else:
1704
+ msg += f"\n❌ 自动返回失败: {return_result['message']}"
1705
+
1706
+ return {
1707
+ "success": True,
1708
+ "message": msg,
1709
+ "app_check": app_check,
1710
+ "return_to_app": return_result
1711
+ }
1528
1712
 
1529
1713
  # 多个相同 ID(<=5个),尝试智能选择
1530
1714
  if count <= 5:
@@ -1537,14 +1721,56 @@ class BasicMobileToolsLite:
1537
1721
  elem.set_text(text)
1538
1722
  time.sleep(0.3)
1539
1723
  self._record_operation('input', element=resource_id, ref=resource_id, text=text)
1540
- return {"success": True, "message": f"✅ 输入成功: '{text}'"}
1724
+
1725
+ # 🎯 关键步骤:检查应用是否跳转,如果跳转则自动返回目标应用
1726
+ app_check = self._check_app_switched()
1727
+ return_result = None
1728
+ if app_check['switched']:
1729
+ return_result = self._return_to_target_app()
1730
+
1731
+ msg = f"✅ 输入成功: '{text}'"
1732
+ if app_check['switched']:
1733
+ msg += f"\n{app_check['message']}"
1734
+ if return_result:
1735
+ if return_result['success']:
1736
+ msg += f"\n{return_result['message']}"
1737
+ else:
1738
+ msg += f"\n❌ 自动返回失败: {return_result['message']}"
1739
+
1740
+ return {
1741
+ "success": True,
1742
+ "message": msg,
1743
+ "app_check": app_check,
1744
+ "return_to_app": return_result
1745
+ }
1541
1746
  except:
1542
1747
  continue
1543
1748
  # 没找到可编辑的,用第一个
1544
1749
  elements[0].set_text(text)
1545
1750
  time.sleep(0.3)
1546
1751
  self._record_operation('input', element=resource_id, ref=resource_id, text=text)
1547
- return {"success": True, "message": f"✅ 输入成功: '{text}'"}
1752
+
1753
+ # 🎯 关键步骤:检查应用是否跳转,如果跳转则自动返回目标应用
1754
+ app_check = self._check_app_switched()
1755
+ return_result = None
1756
+ if app_check['switched']:
1757
+ return_result = self._return_to_target_app()
1758
+
1759
+ msg = f"✅ 输入成功: '{text}'"
1760
+ if app_check['switched']:
1761
+ msg += f"\n{app_check['message']}"
1762
+ if return_result:
1763
+ if return_result['success']:
1764
+ msg += f"\n{return_result['message']}"
1765
+ else:
1766
+ msg += f"\n❌ 自动返回失败: {return_result['message']}"
1767
+
1768
+ return {
1769
+ "success": True,
1770
+ "message": msg,
1771
+ "app_check": app_check,
1772
+ "return_to_app": return_result
1773
+ }
1548
1774
 
1549
1775
  # ID 不可靠(不存在或太多),改用 EditText 类型定位
1550
1776
  edit_texts = self.client.u2(className='android.widget.EditText')
@@ -1554,7 +1780,28 @@ class BasicMobileToolsLite:
1554
1780
  edit_texts.set_text(text)
1555
1781
  time.sleep(0.3)
1556
1782
  self._record_operation('input', element='EditText', ref='EditText', text=text)
1557
- return {"success": True, "message": f"✅ 输入成功: '{text}' (通过 EditText 定位)"}
1783
+
1784
+ # 🎯 关键步骤:检查应用是否跳转,如果跳转则自动返回目标应用
1785
+ app_check = self._check_app_switched()
1786
+ return_result = None
1787
+ if app_check['switched']:
1788
+ return_result = self._return_to_target_app()
1789
+
1790
+ msg = f"✅ 输入成功: '{text}' (通过 EditText 定位)"
1791
+ if app_check['switched']:
1792
+ msg += f"\n{app_check['message']}"
1793
+ if return_result:
1794
+ if return_result['success']:
1795
+ msg += f"\n{return_result['message']}"
1796
+ else:
1797
+ msg += f"\n❌ 自动返回失败: {return_result['message']}"
1798
+
1799
+ return {
1800
+ "success": True,
1801
+ "message": msg,
1802
+ "app_check": app_check,
1803
+ "return_to_app": return_result
1804
+ }
1558
1805
 
1559
1806
  # 多个 EditText,选择最靠上的
1560
1807
  best_elem = None
@@ -1573,7 +1820,28 @@ class BasicMobileToolsLite:
1573
1820
  best_elem.set_text(text)
1574
1821
  time.sleep(0.3)
1575
1822
  self._record_operation('input', element='EditText', ref='EditText', text=text)
1576
- return {"success": True, "message": f"✅ 输入成功: '{text}' (通过 EditText 定位,选择最顶部的)"}
1823
+
1824
+ # 🎯 关键步骤:检查应用是否跳转,如果跳转则自动返回目标应用
1825
+ app_check = self._check_app_switched()
1826
+ return_result = None
1827
+ if app_check['switched']:
1828
+ return_result = self._return_to_target_app()
1829
+
1830
+ msg = f"✅ 输入成功: '{text}' (通过 EditText 定位,选择最顶部的)"
1831
+ if app_check['switched']:
1832
+ msg += f"\n{app_check['message']}"
1833
+ if return_result:
1834
+ if return_result['success']:
1835
+ msg += f"\n{return_result['message']}"
1836
+ else:
1837
+ msg += f"\n❌ 自动返回失败: {return_result['message']}"
1838
+
1839
+ return {
1840
+ "success": True,
1841
+ "message": msg,
1842
+ "app_check": app_check,
1843
+ "return_to_app": return_result
1844
+ }
1577
1845
 
1578
1846
  return {"success": False, "message": f"❌ 输入框不存在: {resource_id}"}
1579
1847
 
@@ -1625,7 +1893,29 @@ class BasicMobileToolsLite:
1625
1893
  text=text
1626
1894
  )
1627
1895
 
1628
- return {"success": True, "message": f"✅ 输入成功: ({x}, {y}) [相对位置: {x_percent}%, {y_percent}%] -> '{text}'"}
1896
+ # 🎯 关键步骤:检查应用是否跳转,如果跳转则自动返回目标应用
1897
+ app_check = self._check_app_switched()
1898
+ return_result = None
1899
+
1900
+ if app_check['switched']:
1901
+ # 应用已跳转,尝试返回目标应用
1902
+ return_result = self._return_to_target_app()
1903
+
1904
+ msg = f"✅ 输入成功: ({x}, {y}) [相对位置: {x_percent}%, {y_percent}%] -> '{text}'"
1905
+ if app_check['switched']:
1906
+ msg += f"\n{app_check['message']}"
1907
+ if return_result:
1908
+ if return_result['success']:
1909
+ msg += f"\n{return_result['message']}"
1910
+ else:
1911
+ msg += f"\n❌ 自动返回失败: {return_result['message']}"
1912
+
1913
+ return {
1914
+ "success": True,
1915
+ "message": msg,
1916
+ "app_check": app_check,
1917
+ "return_to_app": return_result
1918
+ }
1629
1919
  except Exception as e:
1630
1920
  return {"success": False, "message": f"❌ 输入失败: {e}"}
1631
1921
 
@@ -1692,6 +1982,14 @@ class BasicMobileToolsLite:
1692
1982
  record_info['y_percent'] = y_percent
1693
1983
  self._record_operation('swipe', **record_info)
1694
1984
 
1985
+ # 🎯 关键步骤:检查应用是否跳转,如果跳转则自动返回目标应用
1986
+ app_check = self._check_app_switched()
1987
+ return_result = None
1988
+
1989
+ if app_check['switched']:
1990
+ # 应用已跳转,尝试返回目标应用
1991
+ return_result = self._return_to_target_app()
1992
+
1695
1993
  # 构建返回消息
1696
1994
  msg = f"✅ 滑动成功: {direction}"
1697
1995
  if direction in ['left', 'right']:
@@ -1700,7 +1998,21 @@ class BasicMobileToolsLite:
1700
1998
  elif y is not None:
1701
1999
  msg += f" (高度: {y}px)"
1702
2000
 
1703
- return {"success": True, "message": msg}
2001
+ # 如果检测到应用跳转,添加警告和返回结果
2002
+ if app_check['switched']:
2003
+ msg += f"\n{app_check['message']}"
2004
+ if return_result:
2005
+ if return_result['success']:
2006
+ msg += f"\n{return_result['message']}"
2007
+ else:
2008
+ msg += f"\n❌ 自动返回失败: {return_result['message']}"
2009
+
2010
+ return {
2011
+ "success": True,
2012
+ "message": msg,
2013
+ "app_check": app_check,
2014
+ "return_to_app": return_result
2015
+ }
1704
2016
  except Exception as e:
1705
2017
  return {"success": False, "message": f"❌ 滑动失败: {e}"}
1706
2018
 
@@ -1756,11 +2068,22 @@ class BasicMobileToolsLite:
1756
2068
 
1757
2069
  await asyncio.sleep(2)
1758
2070
 
2071
+ # 记录目标应用包名(用于后续监测应用跳转)
2072
+ self.target_package = package_name
2073
+
2074
+ # 验证是否成功启动到目标应用
2075
+ current = self._get_current_package()
2076
+ if current and current != package_name:
2077
+ return {
2078
+ "success": False,
2079
+ "message": f"❌ 启动失败:当前应用为 {current},期望 {package_name}"
2080
+ }
2081
+
1759
2082
  self._record_operation('launch_app', package_name=package_name)
1760
2083
 
1761
2084
  return {
1762
2085
  "success": True,
1763
- "message": f"✅ 已启动: {package_name}\n💡 建议等待 2-3 秒让页面加载"
2086
+ "message": f"✅ 已启动: {package_name}\n💡 建议等待 2-3 秒让页面加载\n📱 已设置应用状态监测"
1764
2087
  }
1765
2088
  except Exception as e:
1766
2089
  return {"success": False, "message": f"❌ 启动失败: {e}"}
@@ -1858,12 +2181,15 @@ class BasicMobileToolsLite:
1858
2181
  return ios_client.list_elements()
1859
2182
  return [{"error": "iOS 暂不支持元素列表,建议使用截图"}]
1860
2183
  else:
1861
- xml_string = self._get_full_hierarchy()
2184
+ xml_string = self.client.u2.dump_hierarchy(compressed=False)
1862
2185
  elements = self.client.xml_parser.parse(xml_string)
1863
2186
 
1864
2187
  result = []
1865
2188
  for elem in elements:
1866
- if elem.get('clickable') or elem.get('focusable'):
2189
+ # 获取文本内容(去除首尾空格)
2190
+ text = elem.get('text', '').strip()
2191
+ # 保留:可点击、可focus或有文本的元素
2192
+ if elem.get('clickable') or elem.get('focusable') or text:
1867
2193
  result.append({
1868
2194
  'resource_id': elem.get('resource_id', ''),
1869
2195
  'text': elem.get('text', ''),
@@ -1894,8 +2220,8 @@ class BasicMobileToolsLite:
1894
2220
  screen_width = self.client.u2.info.get('displayWidth', 720)
1895
2221
  screen_height = self.client.u2.info.get('displayHeight', 1280)
1896
2222
 
1897
- # 获取元素列表(使用完整 UI 层级)
1898
- xml_string = self._get_full_hierarchy()
2223
+ # 获取元素列表
2224
+ xml_string = self.client.u2.dump_hierarchy(compressed=False)
1899
2225
  import xml.etree.ElementTree as ET
1900
2226
  root = ET.fromstring(xml_string)
1901
2227
 
@@ -2048,8 +2374,8 @@ class BasicMobileToolsLite:
2048
2374
  screen_width = self.client.u2.info.get('displayWidth', 720)
2049
2375
  screen_height = self.client.u2.info.get('displayHeight', 1280)
2050
2376
 
2051
- # 获取原始 XML(使用完整 UI 层级)
2052
- xml_string = self._get_full_hierarchy()
2377
+ # 获取原始 XML
2378
+ xml_string = self.client.u2.dump_hierarchy(compressed=False)
2053
2379
 
2054
2380
  # 关闭按钮的文本特征
2055
2381
  close_texts = ['×', 'X', 'x', '关闭', '取消', 'close', 'Close', 'CLOSE', '跳过', '知道了']
@@ -2278,13 +2604,33 @@ class BasicMobileToolsLite:
2278
2604
  self.client.u2.click(try_x, try_y)
2279
2605
  time.sleep(0.3)
2280
2606
 
2607
+ # 🎯 关键步骤:检查应用是否跳转,如果跳转说明弹窗去除失败,需要返回目标应用
2608
+ app_check = self._check_app_switched()
2609
+ return_result = None
2610
+
2611
+ if app_check['switched']:
2612
+ # 应用已跳转,说明弹窗去除失败,尝试返回目标应用
2613
+ return_result = self._return_to_target_app()
2614
+
2281
2615
  # 尝试后截图,让 AI 判断是否成功
2282
2616
  screenshot_result = self.take_screenshot("尝试关闭后")
2617
+
2618
+ msg = f"✅ 已尝试点击常见关闭按钮位置"
2619
+ if app_check['switched']:
2620
+ msg += f"\n⚠️ 应用已跳转,说明弹窗去除失败"
2621
+ if return_result:
2622
+ if return_result['success']:
2623
+ msg += f"\n{return_result['message']}"
2624
+ else:
2625
+ msg += f"\n❌ 自动返回失败: {return_result['message']}"
2626
+
2283
2627
  return {
2284
2628
  "success": True,
2285
- "message": f"✅ 已尝试点击常见关闭按钮位置",
2629
+ "message": msg,
2286
2630
  "tried_positions": [p[2] for p in try_positions],
2287
2631
  "screenshot": screenshot_result.get("screenshot_path", ""),
2632
+ "app_check": app_check,
2633
+ "return_to_app": return_result,
2288
2634
  "tip": "请查看截图确认弹窗是否已关闭。如果还在,可手动分析截图找到关闭按钮位置。"
2289
2635
  }
2290
2636
 
@@ -2317,6 +2663,14 @@ class BasicMobileToolsLite:
2317
2663
  self.client.u2.click(best['center_x'], best['center_y'])
2318
2664
  time.sleep(0.5)
2319
2665
 
2666
+ # 🎯 关键步骤:检查应用是否跳转,如果跳转说明弹窗去除失败,需要返回目标应用
2667
+ app_check = self._check_app_switched()
2668
+ return_result = None
2669
+
2670
+ if app_check['switched']:
2671
+ # 应用已跳转,说明弹窗去除失败,尝试返回目标应用
2672
+ return_result = self._return_to_target_app()
2673
+
2320
2674
  # 点击后截图,让 AI 判断是否成功
2321
2675
  screenshot_result = self.take_screenshot("关闭弹窗后")
2322
2676
 
@@ -2332,11 +2686,21 @@ class BasicMobileToolsLite:
2332
2686
  ref=f"close_popup_{best['position']}"
2333
2687
  )
2334
2688
 
2689
+ # 构建返回消息
2690
+ msg = f"✅ 已点击关闭按钮 ({best['position']}): ({best['center_x']}, {best['center_y']})"
2691
+ if app_check['switched']:
2692
+ msg += f"\n⚠️ 应用已跳转,说明弹窗去除失败"
2693
+ if return_result:
2694
+ if return_result['success']:
2695
+ msg += f"\n{return_result['message']}"
2696
+ else:
2697
+ msg += f"\n❌ 自动返回失败: {return_result['message']}"
2698
+
2335
2699
  # 返回候选按钮列表,让 AI 看截图判断
2336
2700
  # 如果弹窗还在,AI 可以选择点击其他候选按钮
2337
2701
  return {
2338
2702
  "success": True,
2339
- "message": f"✅ 已点击关闭按钮 ({best['position']}): ({best['center_x']}, {best['center_y']})",
2703
+ "message": msg,
2340
2704
  "clicked": {
2341
2705
  "position": best['position'],
2342
2706
  "match_type": best['match_type'],
@@ -2346,6 +2710,8 @@ class BasicMobileToolsLite:
2346
2710
  "screenshot": screenshot_result.get("screenshot_path", ""),
2347
2711
  "popup_detected": popup_bounds is not None,
2348
2712
  "popup_bounds": f"[{popup_bounds[0]},{popup_bounds[1]}][{popup_bounds[2]},{popup_bounds[3]}]" if popup_bounds else None,
2713
+ "app_check": app_check,
2714
+ "return_to_app": return_result,
2349
2715
  "other_candidates": [
2350
2716
  {
2351
2717
  "position": c['position'],
@@ -2355,7 +2721,7 @@ class BasicMobileToolsLite:
2355
2721
  }
2356
2722
  for c in close_candidates[1:4] # 返回其他3个候选,AI 可以选择
2357
2723
  ],
2358
- "tip": "请查看截图判断弹窗是否已关闭。如果弹窗还在,可以尝试点击 other_candidates 中的其他位置;如果误点跳转了,请按返回键"
2724
+ "tip": "请查看截图判断弹窗是否已关闭。如果弹窗还在,可以尝试点击 other_candidates 中的其他位置"
2359
2725
  }
2360
2726
 
2361
2727
  except Exception as e:
@@ -2920,8 +3286,8 @@ class BasicMobileToolsLite:
2920
3286
  try:
2921
3287
  import xml.etree.ElementTree as ET
2922
3288
 
2923
- # ========== 第1步:控件树查找关闭按钮(使用完整 UI 层级)==========
2924
- xml_string = self._get_full_hierarchy()
3289
+ # ========== 第1步:控件树查找关闭按钮 ==========
3290
+ xml_string = self.client.u2.dump_hierarchy(compressed=False)
2925
3291
  root = ET.fromstring(xml_string)
2926
3292
 
2927
3293
  # 关闭按钮的常见特征
@@ -3008,15 +3374,35 @@ class BasicMobileToolsLite:
3008
3374
  pre_result = self.take_screenshot(description="关闭前", compress=False)
3009
3375
  pre_screenshot = pre_result.get("screenshot_path")
3010
3376
 
3011
- # 点击
3012
- self.click_at_coords(cx, cy)
3377
+ # 点击(click_at_coords 内部已包含应用状态检查和自动返回)
3378
+ click_result = self.click_at_coords(cx, cy)
3013
3379
  time.sleep(0.5)
3014
3380
 
3381
+ # 🎯 再次检查应用状态(确保弹窗去除没有导致应用跳转)
3382
+ app_check = self._check_app_switched()
3383
+ return_result = None
3384
+
3385
+ if app_check['switched']:
3386
+ # 应用已跳转,说明弹窗去除失败,尝试返回目标应用
3387
+ return_result = self._return_to_target_app()
3388
+
3015
3389
  result["success"] = True
3016
3390
  result["method"] = "控件树"
3017
- result["message"] = f"✅ 通过控件树找到关闭按钮并点击\n" \
3018
- f" 位置: ({cx}, {cy})\n" \
3019
- f" 原因: {best['reason']}"
3391
+ msg = f"✅ 通过控件树找到关闭按钮并点击\n" \
3392
+ f" 位置: ({cx}, {cy})\n" \
3393
+ f" 原因: {best['reason']}"
3394
+
3395
+ if app_check['switched']:
3396
+ msg += f"\n⚠️ 应用已跳转,说明弹窗去除失败"
3397
+ if return_result:
3398
+ if return_result['success']:
3399
+ msg += f"\n{return_result['message']}"
3400
+ else:
3401
+ msg += f"\n❌ 自动返回失败: {return_result['message']}"
3402
+
3403
+ result["message"] = msg
3404
+ result["app_check"] = app_check
3405
+ result["return_to_app"] = return_result
3020
3406
 
3021
3407
  # 自动学习:检查这个 X 是否已在模板库,不在就添加
3022
3408
  if auto_learn and pre_screenshot:
@@ -3046,16 +3432,36 @@ class BasicMobileToolsLite:
3046
3432
  x_pct = best["percent"]["x"]
3047
3433
  y_pct = best["percent"]["y"]
3048
3434
 
3049
- # 点击
3050
- self.click_by_percent(x_pct, y_pct)
3435
+ # 点击(click_by_percent 内部已包含应用状态检查和自动返回)
3436
+ click_result = self.click_by_percent(x_pct, y_pct)
3051
3437
  time.sleep(0.5)
3052
3438
 
3439
+ # 🎯 再次检查应用状态(确保弹窗去除没有导致应用跳转)
3440
+ app_check = self._check_app_switched()
3441
+ return_result = None
3442
+
3443
+ if app_check['switched']:
3444
+ # 应用已跳转,说明弹窗去除失败,尝试返回目标应用
3445
+ return_result = self._return_to_target_app()
3446
+
3053
3447
  result["success"] = True
3054
3448
  result["method"] = "模板匹配"
3055
- result["message"] = f"✅ 通过模板匹配找到关闭按钮并点击\n" \
3056
- f" 模板: {best.get('template', 'unknown')}\n" \
3057
- f" 置信度: {best.get('confidence', 'N/A')}%\n" \
3058
- f" 位置: ({x_pct:.1f}%, {y_pct:.1f}%)"
3449
+ msg = f"✅ 通过模板匹配找到关闭按钮并点击\n" \
3450
+ f" 模板: {best.get('template', 'unknown')}\n" \
3451
+ f" 置信度: {best.get('confidence', 'N/A')}%\n" \
3452
+ f" 位置: ({x_pct:.1f}%, {y_pct:.1f}%)"
3453
+
3454
+ if app_check['switched']:
3455
+ msg += f"\n⚠️ 应用已跳转,说明弹窗去除失败"
3456
+ if return_result:
3457
+ if return_result['success']:
3458
+ msg += f"\n{return_result['message']}"
3459
+ else:
3460
+ msg += f"\n❌ 自动返回失败: {return_result['message']}"
3461
+
3462
+ result["message"] = msg
3463
+ result["app_check"] = app_check
3464
+ result["return_to_app"] = return_result
3059
3465
  return result
3060
3466
 
3061
3467
  except ImportError:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mobile-mcp-ai
3
- Version: 2.5.9
3
+ Version: 2.5.10
4
4
  Summary: 移动端自动化 MCP Server - 支持 Android/iOS,AI 功能可选(基础工具不需要 AI)
5
5
  Home-page: https://github.com/test111ddff-hash/mobile-mcp-ai
6
6
  Author: douzi
@@ -1,7 +1,7 @@
1
1
  mobile_mcp/__init__.py,sha256=sQJZTL_sxQFzmcS7jOtS2AHCfUySz40vhX96N6u1qy4,816
2
2
  mobile_mcp/config.py,sha256=yaFLAV4bc2wX0GQPtZDo7OYF9E88tXV-av41fQsJwK4,4480
3
3
  mobile_mcp/core/__init__.py,sha256=ndMy-cLAIsQDG5op7gM_AIplycqZSZPWEkec1pEhvEY,170
4
- mobile_mcp/core/basic_tools_lite.py,sha256=C9_mptagHKGgGfPUxzmoM0t080JNERYDDNb0OrbV8sQ,154736
4
+ mobile_mcp/core/basic_tools_lite.py,sha256=YwOSRYRyYo0etA_4XbeDLGIlMNIquWAWOC8ROcPog4E,173551
5
5
  mobile_mcp/core/device_manager.py,sha256=xG5DoeNFs45pl-FTEhEWblqVwxtFK-FmVEGlNL6EqRI,8798
6
6
  mobile_mcp/core/dynamic_config.py,sha256=Ja1n1pfb0HspGByqk2_A472mYVniKmGtNEWyjUjmgK8,9811
7
7
  mobile_mcp/core/ios_client_wda.py,sha256=Nq9WxevhTWpVpolM-Ymp-b0nUQV3tXLFszmJHbDC4wA,18770
@@ -24,9 +24,9 @@ mobile_mcp/utils/__init__.py,sha256=8EH0i7UGtx1y_j_GEgdN-cZdWn2sRtZSEOLlNF9HRnY,
24
24
  mobile_mcp/utils/logger.py,sha256=Sqq2Nr0Y4p03erqcrbYKVPCGiFaNGHMcE_JwCkeOfU4,3626
25
25
  mobile_mcp/utils/xml_formatter.py,sha256=uwTRb3vLbqhT8O-udzWT7s7LsV-DyDUz2DkofD3hXOE,4556
26
26
  mobile_mcp/utils/xml_parser.py,sha256=QhL8CWbdmNDzmBLjtx6mEnjHgMFZzJeHpCL15qfXSpI,3926
27
- mobile_mcp_ai-2.5.9.dist-info/licenses/LICENSE,sha256=HrhfyXIkWY2tGFK11kg7vPCqhgh5DcxleloqdhrpyMY,11558
28
- mobile_mcp_ai-2.5.9.dist-info/METADATA,sha256=j1KcGXfXaAeU3KJiSOAlo150FG9Mvg3cRlmDjJK3nZY,10495
29
- mobile_mcp_ai-2.5.9.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
30
- mobile_mcp_ai-2.5.9.dist-info/entry_points.txt,sha256=KB_FglozgPHBprSM1vFbIzGyheFuHFmGanscRdMJ_8A,68
31
- mobile_mcp_ai-2.5.9.dist-info/top_level.txt,sha256=lLm6YpbTv855Lbh8BIA0rPxhybIrvYUzMEk9OErHT94,11
32
- mobile_mcp_ai-2.5.9.dist-info/RECORD,,
27
+ mobile_mcp_ai-2.5.10.dist-info/licenses/LICENSE,sha256=HrhfyXIkWY2tGFK11kg7vPCqhgh5DcxleloqdhrpyMY,11558
28
+ mobile_mcp_ai-2.5.10.dist-info/METADATA,sha256=NsXkw6Ys3YGOXDt7qA82-F9EOuYQDtawMFvzclzDpVk,10496
29
+ mobile_mcp_ai-2.5.10.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
30
+ mobile_mcp_ai-2.5.10.dist-info/entry_points.txt,sha256=KB_FglozgPHBprSM1vFbIzGyheFuHFmGanscRdMJ_8A,68
31
+ mobile_mcp_ai-2.5.10.dist-info/top_level.txt,sha256=lLm6YpbTv855Lbh8BIA0rPxhybIrvYUzMEk9OErHT94,11
32
+ mobile_mcp_ai-2.5.10.dist-info/RECORD,,