mobile-mcp-ai 2.4.1__py3-none-any.whl → 2.4.3__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.
@@ -53,6 +53,8 @@ class BasicMobileToolsLite:
53
53
  }
54
54
  self.operation_history.append(record)
55
55
 
56
+
57
+
56
58
  # ==================== 截图 ====================
57
59
 
58
60
  def take_screenshot(self, description: str = "", compress: bool = True,
@@ -277,6 +279,554 @@ class BasicMobileToolsLite:
277
279
  except Exception as e:
278
280
  return {"success": False, "message": f"❌ 截图失败: {e}"}
279
281
 
282
+ def take_screenshot_with_grid(self, grid_size: int = 100, show_popup_hints: bool = True) -> Dict:
283
+ """截图并添加网格坐标标注(用于精确定位元素)
284
+
285
+ 在截图上绘制网格线和坐标刻度,帮助快速定位元素位置。
286
+ 如果检测到弹窗,会标注弹窗区域和可能的关闭按钮位置。
287
+
288
+ Args:
289
+ grid_size: 网格间距(像素),默认 100。建议值:50-200
290
+ show_popup_hints: 是否显示弹窗关闭按钮提示位置,默认 True
291
+
292
+ Returns:
293
+ 包含标注截图路径和弹窗信息的字典
294
+ """
295
+ try:
296
+ from PIL import Image, ImageDraw, ImageFont
297
+ import re
298
+
299
+ timestamp = time.strftime("%Y%m%d_%H%M%S")
300
+ platform = "ios" if self._is_ios() else "android"
301
+
302
+ # 第1步:截图
303
+ temp_filename = f"temp_grid_{timestamp}.png"
304
+ temp_path = self.screenshot_dir / temp_filename
305
+
306
+ screen_width, screen_height = 0, 0
307
+ if self._is_ios():
308
+ ios_client = self._get_ios_client()
309
+ if ios_client and hasattr(ios_client, 'wda'):
310
+ ios_client.wda.screenshot(str(temp_path))
311
+ size = ios_client.wda.window_size()
312
+ screen_width, screen_height = size[0], size[1]
313
+ else:
314
+ return {"success": False, "message": "❌ iOS 客户端未初始化"}
315
+ else:
316
+ self.client.u2.screenshot(str(temp_path))
317
+ info = self.client.u2.info
318
+ screen_width = info.get('displayWidth', 720)
319
+ screen_height = info.get('displayHeight', 1280)
320
+
321
+ img = Image.open(temp_path)
322
+ draw = ImageDraw.Draw(img, 'RGBA')
323
+
324
+ # 尝试加载字体
325
+ try:
326
+ font = ImageFont.truetype("/System/Library/Fonts/Helvetica.ttc", 14)
327
+ font_small = ImageFont.truetype("/System/Library/Fonts/Helvetica.ttc", 11)
328
+ except:
329
+ font = ImageFont.load_default()
330
+ font_small = font
331
+
332
+ img_width, img_height = img.size
333
+
334
+ # 第2步:绘制网格线和坐标
335
+ grid_color = (255, 0, 0, 80) # 半透明红色
336
+ text_color = (255, 0, 0, 200) # 红色文字
337
+
338
+ # 绘制垂直网格线
339
+ for x in range(0, img_width, grid_size):
340
+ draw.line([(x, 0), (x, img_height)], fill=grid_color, width=1)
341
+ # 顶部标注 X 坐标
342
+ draw.text((x + 2, 2), str(x), fill=text_color, font=font_small)
343
+
344
+ # 绘制水平网格线
345
+ for y in range(0, img_height, grid_size):
346
+ draw.line([(0, y), (img_width, y)], fill=grid_color, width=1)
347
+ # 左侧标注 Y 坐标
348
+ draw.text((2, y + 2), str(y), fill=text_color, font=font_small)
349
+
350
+ # 第3步:检测弹窗并标注
351
+ popup_info = None
352
+ close_positions = []
353
+
354
+ if show_popup_hints and not self._is_ios():
355
+ try:
356
+ import xml.etree.ElementTree as ET
357
+ xml_string = self.client.u2.dump_hierarchy()
358
+ root = ET.fromstring(xml_string)
359
+
360
+ # 检测弹窗区域
361
+ popup_bounds = None
362
+ for elem in root.iter():
363
+ bounds_str = elem.attrib.get('bounds', '')
364
+ class_name = elem.attrib.get('class', '')
365
+
366
+ if not bounds_str:
367
+ continue
368
+
369
+ match = re.match(r'\[(\d+),(\d+)\]\[(\d+),(\d+)\]', bounds_str)
370
+ if not match:
371
+ continue
372
+
373
+ x1, y1, x2, y2 = map(int, match.groups())
374
+ width = x2 - x1
375
+ height = y2 - y1
376
+ area = width * height
377
+ screen_area = screen_width * screen_height
378
+
379
+ is_container = any(kw in class_name for kw in ['Layout', 'View', 'Dialog', 'Card'])
380
+ area_ratio = area / screen_area if screen_area > 0 else 0
381
+ is_not_fullscreen = (width < screen_width * 0.98 or height < screen_height * 0.98)
382
+ is_reasonable_size = 0.08 < area_ratio < 0.85
383
+
384
+ if is_container and is_not_fullscreen and is_reasonable_size and y1 > 50:
385
+ if popup_bounds is None or area > (popup_bounds[2] - popup_bounds[0]) * (popup_bounds[3] - popup_bounds[1]):
386
+ popup_bounds = (x1, y1, x2, y2)
387
+
388
+ if popup_bounds:
389
+ px1, py1, px2, py2 = popup_bounds
390
+ popup_width = px2 - px1
391
+ popup_height = py2 - py1
392
+
393
+ # 绘制弹窗边框(蓝色)
394
+ draw.rectangle([px1, py1, px2, py2], outline=(0, 100, 255, 200), width=3)
395
+ draw.text((px1 + 5, py1 + 5), f"弹窗区域", fill=(0, 100, 255), font=font)
396
+
397
+ # 计算可能的 X 按钮位置(基于弹窗尺寸动态计算,适配不同分辨率)
398
+ offset_x = max(25, int(popup_width * 0.05)) # 宽度的5%,最小25px
399
+ offset_y = max(25, int(popup_height * 0.04)) # 高度的4%,最小25px
400
+ outer_offset = max(15, int(popup_width * 0.025)) # 外部偏移
401
+
402
+ close_positions = [
403
+ {"name": "右上角内", "x": px2 - offset_x, "y": py1 + offset_y, "priority": 1},
404
+ {"name": "右上角外", "x": px2 + outer_offset, "y": py1 - outer_offset, "priority": 2},
405
+ {"name": "正上方", "x": (px1 + px2) // 2, "y": py1 - offset_y, "priority": 3},
406
+ {"name": "底部下方", "x": (px1 + px2) // 2, "y": py2 + offset_y, "priority": 4},
407
+ ]
408
+
409
+ # 绘制可能的 X 按钮位置(绿色圆圈 + 数字)
410
+ for i, pos in enumerate(close_positions):
411
+ cx, cy = pos["x"], pos["y"]
412
+ if 0 <= cx <= img_width and 0 <= cy <= img_height:
413
+ # 绿色圆圈
414
+ draw.ellipse([cx-15, cy-15, cx+15, cy+15],
415
+ outline=(0, 255, 0, 200), width=2)
416
+ # 数字标注
417
+ draw.text((cx-5, cy-8), str(i+1), fill=(0, 255, 0), font=font)
418
+ # 坐标标注
419
+ draw.text((cx+18, cy-8), f"({cx},{cy})", fill=(0, 255, 0), font=font_small)
420
+
421
+ popup_info = {
422
+ "bounds": f"[{px1},{py1}][{px2},{py2}]",
423
+ "width": px2 - px1,
424
+ "height": py2 - py1,
425
+ "close_positions": close_positions
426
+ }
427
+
428
+ except Exception as e:
429
+ pass # 弹窗检测失败不影响主功能
430
+
431
+ # 第4步:保存标注后的截图
432
+ filename = f"screenshot_{platform}_grid_{timestamp}.jpg"
433
+ final_path = self.screenshot_dir / filename
434
+
435
+ # 转换为 RGB 并保存
436
+ if img.mode in ('RGBA', 'LA', 'P'):
437
+ background = Image.new('RGB', img.size, (255, 255, 255))
438
+ if img.mode == 'P':
439
+ img = img.convert('RGBA')
440
+ background.paste(img, mask=img.split()[-1] if img.mode == 'RGBA' else None)
441
+ img = background
442
+ elif img.mode != 'RGB':
443
+ img = img.convert("RGB")
444
+
445
+ img.save(str(final_path), "JPEG", quality=85)
446
+ temp_path.unlink()
447
+
448
+ result = {
449
+ "success": True,
450
+ "screenshot_path": str(final_path),
451
+ "screen_width": screen_width,
452
+ "screen_height": screen_height,
453
+ "image_width": img_width,
454
+ "image_height": img_height,
455
+ "grid_size": grid_size,
456
+ "message": f"📸 网格截图已保存: {final_path}\n"
457
+ f"📐 尺寸: {img_width}x{img_height}\n"
458
+ f"📏 网格间距: {grid_size}px"
459
+ }
460
+
461
+ if popup_info:
462
+ result["popup_detected"] = True
463
+ result["popup_bounds"] = popup_info["bounds"]
464
+ result["close_button_hints"] = close_positions
465
+ result["message"] += f"\n🎯 检测到弹窗: {popup_info['bounds']}"
466
+ result["message"] += f"\n💡 可能的关闭按钮位置(绿色圆圈标注):"
467
+ for pos in close_positions:
468
+ result["message"] += f"\n {pos['priority']}. {pos['name']}: ({pos['x']}, {pos['y']})"
469
+ else:
470
+ result["popup_detected"] = False
471
+
472
+ return result
473
+
474
+ except ImportError:
475
+ return {"success": False, "message": "❌ 需要安装 Pillow: pip install Pillow"}
476
+ except Exception as e:
477
+ return {"success": False, "message": f"❌ 网格截图失败: {e}"}
478
+
479
+ def take_screenshot_with_som(self) -> Dict:
480
+ """Set-of-Mark 截图:给每个可点击元素标上数字(超级好用!)
481
+
482
+ 在截图上给每个可点击元素画框并标上数字编号。
483
+ AI 看图后直接说"点击 3 号",然后调用 click_by_som(3) 即可。
484
+
485
+ Returns:
486
+ 包含标注截图和元素列表的字典
487
+ """
488
+ try:
489
+ from PIL import Image, ImageDraw, ImageFont
490
+ import re
491
+
492
+ timestamp = time.strftime("%Y%m%d_%H%M%S")
493
+ platform = "ios" if self._is_ios() else "android"
494
+
495
+ # 第1步:截图
496
+ temp_filename = f"temp_som_{timestamp}.png"
497
+ temp_path = self.screenshot_dir / temp_filename
498
+
499
+ screen_width, screen_height = 0, 0
500
+ if self._is_ios():
501
+ ios_client = self._get_ios_client()
502
+ if ios_client and hasattr(ios_client, 'wda'):
503
+ ios_client.wda.screenshot(str(temp_path))
504
+ size = ios_client.wda.window_size()
505
+ screen_width, screen_height = size[0], size[1]
506
+ else:
507
+ return {"success": False, "message": "❌ iOS 客户端未初始化"}
508
+ else:
509
+ self.client.u2.screenshot(str(temp_path))
510
+ info = self.client.u2.info
511
+ screen_width = info.get('displayWidth', 720)
512
+ screen_height = info.get('displayHeight', 1280)
513
+
514
+ img = Image.open(temp_path)
515
+ draw = ImageDraw.Draw(img, 'RGBA')
516
+ img_width, img_height = img.size
517
+
518
+ # 尝试加载字体
519
+ try:
520
+ font = ImageFont.truetype("/System/Library/Fonts/Helvetica.ttc", 16)
521
+ font_small = ImageFont.truetype("/System/Library/Fonts/Helvetica.ttc", 12)
522
+ except:
523
+ font = ImageFont.load_default()
524
+ font_small = font
525
+
526
+ # 第2步:获取所有可点击元素
527
+ elements = []
528
+ if self._is_ios():
529
+ # iOS 暂不支持
530
+ pass
531
+ else:
532
+ try:
533
+ import xml.etree.ElementTree as ET
534
+ xml_string = self.client.u2.dump_hierarchy()
535
+ root = ET.fromstring(xml_string)
536
+
537
+ for elem in root.iter():
538
+ clickable = elem.attrib.get('clickable', 'false') == 'true'
539
+ bounds_str = elem.attrib.get('bounds', '')
540
+ text = elem.attrib.get('text', '')
541
+ content_desc = elem.attrib.get('content-desc', '')
542
+ resource_id = elem.attrib.get('resource-id', '')
543
+ class_name = elem.attrib.get('class', '')
544
+
545
+ if not clickable or not bounds_str:
546
+ continue
547
+
548
+ match = re.match(r'\[(\d+),(\d+)\]\[(\d+),(\d+)\]', bounds_str)
549
+ if not match:
550
+ continue
551
+
552
+ x1, y1, x2, y2 = map(int, match.groups())
553
+ width = x2 - x1
554
+ height = y2 - y1
555
+
556
+ # 过滤太小或太大的元素
557
+ if width < 20 or height < 20:
558
+ continue
559
+ if width >= screen_width * 0.98 and height >= screen_height * 0.5:
560
+ continue # 全屏或大面积容器
561
+
562
+ center_x = (x1 + x2) // 2
563
+ center_y = (y1 + y2) // 2
564
+
565
+ # 生成描述
566
+ desc = text or content_desc or resource_id.split('/')[-1] if resource_id else class_name.split('.')[-1]
567
+ if len(desc) > 20:
568
+ desc = desc[:17] + "..."
569
+
570
+ elements.append({
571
+ 'bounds': (x1, y1, x2, y2),
572
+ 'center': (center_x, center_y),
573
+ 'text': text,
574
+ 'desc': desc,
575
+ 'resource_id': resource_id
576
+ })
577
+ except Exception as e:
578
+ pass
579
+
580
+ # 第3步:在截图上标注元素
581
+ # 颜色列表(循环使用)
582
+ colors = [
583
+ (255, 0, 0), # 红
584
+ (0, 255, 0), # 绿
585
+ (0, 100, 255), # 蓝
586
+ (255, 165, 0), # 橙
587
+ (255, 0, 255), # 紫
588
+ (0, 255, 255), # 青
589
+ ]
590
+
591
+ som_elements = [] # 保存标注信息,供 click_by_som 使用
592
+
593
+ for i, elem in enumerate(elements):
594
+ x1, y1, x2, y2 = elem['bounds']
595
+ cx, cy = elem['center']
596
+ color = colors[i % len(colors)]
597
+
598
+ # 画边框
599
+ draw.rectangle([x1, y1, x2, y2], outline=color + (200,), width=2)
600
+
601
+ # 画编号标签背景
602
+ label = str(i + 1)
603
+ label_w, label_h = 20, 18
604
+ label_x = x1
605
+ label_y = max(0, y1 - label_h - 2)
606
+ draw.rectangle([label_x, label_y, label_x + label_w, label_y + label_h],
607
+ fill=color + (220,))
608
+
609
+ # 画编号文字
610
+ draw.text((label_x + 4, label_y + 1), label, fill=(255, 255, 255), font=font_small)
611
+
612
+ som_elements.append({
613
+ 'index': i + 1,
614
+ 'center': (cx, cy),
615
+ 'bounds': f"[{x1},{y1}][{x2},{y2}]",
616
+ 'desc': elem['desc']
617
+ })
618
+
619
+ # 第3.5步:检测弹窗并标注可能的 X 按钮位置(如果 X 不在元素树中)
620
+ popup_bounds = None
621
+ popup_close_hints = []
622
+
623
+ if not self._is_ios():
624
+ try:
625
+ # 检测弹窗区域
626
+ for elem in root.iter():
627
+ bounds_str = elem.attrib.get('bounds', '')
628
+ class_name = elem.attrib.get('class', '')
629
+
630
+ if not bounds_str:
631
+ continue
632
+
633
+ match = re.match(r'\[(\d+),(\d+)\]\[(\d+),(\d+)\]', bounds_str)
634
+ if not match:
635
+ continue
636
+
637
+ px1, py1, px2, py2 = map(int, match.groups())
638
+ p_width = px2 - px1
639
+ p_height = py2 - py1
640
+ p_area = p_width * p_height
641
+ screen_area = screen_width * screen_height
642
+
643
+ is_container = any(kw in class_name for kw in ['Layout', 'View', 'Dialog', 'Card', 'Frame'])
644
+ area_ratio = p_area / screen_area if screen_area > 0 else 0
645
+ is_not_fullscreen = (p_width < screen_width * 0.99 or p_height < screen_height * 0.95)
646
+ # 放宽面积范围:5% - 95%
647
+ is_reasonable_size = 0.05 < area_ratio < 0.95
648
+
649
+ if is_container and is_not_fullscreen and is_reasonable_size and py1 > 30:
650
+ if popup_bounds is None or p_area > (popup_bounds[2] - popup_bounds[0]) * (popup_bounds[3] - popup_bounds[1]):
651
+ popup_bounds = (px1, py1, px2, py2)
652
+
653
+ # 如果检测到弹窗,始终添加 X 按钮位置提示
654
+ if popup_bounds:
655
+ px1, py1, px2, py2 = popup_bounds
656
+ popup_width = px2 - px1
657
+ popup_height = py2 - py1
658
+
659
+ # 计算多个可能的 X 按钮位置(基于弹窗尺寸动态计算)
660
+ # 【优化】X按钮有三种常见位置:
661
+ # 1. 弹窗边界上方(浮动X按钮)
662
+ # 2. 弹窗内靠近顶部边界(内嵌X按钮)
663
+ # 3. 弹窗正下方(底部关闭按钮)
664
+ offset_x = max(60, int(popup_width * 0.07)) # 宽度7%,距右边界
665
+ offset_y_above = max(35, int(popup_height * 0.025)) # 高度2.5%,在顶边界之上
666
+ offset_y_near = max(45, int(popup_height * 0.03)) # 高度3%,紧贴顶边界内侧
667
+
668
+ close_positions = [
669
+ # 【最高优先级】弹窗内紧贴顶部边界(大多数X按钮在这里)
670
+ {"name": "右上角", "x": px2 - offset_x, "y": py1 + offset_y_near},
671
+ # 弹窗边界上方(浮动X按钮)
672
+ {"name": "右上浮", "x": px2 - offset_x, "y": py1 - offset_y_above},
673
+ # 弹窗正下方中间(底部关闭按钮)
674
+ {"name": "正下方", "x": (px1 + px2) // 2, "y": py2 + max(50, int(popup_height * 0.04))},
675
+ ]
676
+
677
+ # 用黄色/金色标注这些可能位置(始终显示)
678
+ hint_color = (255, 200, 0) # 金黄色
679
+ next_index = len(som_elements) + 1
680
+
681
+ for pos in close_positions:
682
+ hx, hy = pos["x"], pos["y"]
683
+ if 0 <= hx <= img_width and 0 <= hy <= img_height:
684
+ # 画圆圈
685
+ draw.ellipse([hx-18, hy-18, hx+18, hy+18],
686
+ outline=hint_color + (255,), width=3)
687
+ # 画编号背景
688
+ draw.rectangle([hx-10, hy-22, hx+10, hy-6],
689
+ fill=hint_color + (220,))
690
+ # 画编号
691
+ draw.text((hx-6, hy-20), str(next_index),
692
+ fill=(0, 0, 0), font=font_small)
693
+ # 标注 "X?"
694
+ draw.text((hx-8, hy-5), "X?", fill=hint_color, font=font_small)
695
+
696
+ popup_close_hints.append({
697
+ 'index': next_index,
698
+ 'center': (hx, hy),
699
+ 'bounds': f"[{hx-20},{hy-20}][{hx+20},{hy+20}]",
700
+ 'desc': f"X?{pos['name']}",
701
+ 'is_hint': True
702
+ })
703
+ next_index += 1
704
+
705
+ # 画弹窗边框(蓝色)
706
+ draw.rectangle([px1, py1, px2, py2], outline=(0, 150, 255, 180), width=2)
707
+
708
+ except Exception as e:
709
+ pass # 弹窗检测失败不影响主功能
710
+
711
+ # 合并元素列表
712
+ all_som_elements = som_elements + popup_close_hints
713
+
714
+ # 保存到实例变量,供 click_by_som 使用
715
+ self._som_elements = all_som_elements
716
+
717
+ # 第4步:保存标注后的截图
718
+ filename = f"screenshot_{platform}_som_{timestamp}.jpg"
719
+ final_path = self.screenshot_dir / filename
720
+
721
+ if img.mode in ('RGBA', 'LA', 'P'):
722
+ background = Image.new('RGB', img.size, (255, 255, 255))
723
+ if img.mode == 'P':
724
+ img = img.convert('RGBA')
725
+ background.paste(img, mask=img.split()[-1] if img.mode == 'RGBA' else None)
726
+ img = background
727
+ elif img.mode != 'RGB':
728
+ img = img.convert("RGB")
729
+
730
+ img.save(str(final_path), "JPEG", quality=85)
731
+ temp_path.unlink()
732
+
733
+ # 构建元素列表文字
734
+ elements_text = "\n".join([
735
+ f" [{e['index']}] {e['desc']} → ({e['center'][0]}, {e['center'][1]})"
736
+ for e in som_elements[:15] # 只显示前15个
737
+ ])
738
+ if len(som_elements) > 15:
739
+ elements_text += f"\n ... 还有 {len(som_elements) - 15} 个元素"
740
+
741
+ # 构建弹窗提示文字
742
+ hints_text = ""
743
+ if popup_close_hints:
744
+ hints_text = "\n🎯 检测到弹窗,可能的 X 按钮位置(黄色圆圈):\n"
745
+ hints_text += "\n".join([
746
+ f" [{h['index']}] {h['desc']} → ({h['center'][0]}, {h['center'][1]})"
747
+ for h in popup_close_hints
748
+ ])
749
+
750
+ return {
751
+ "success": True,
752
+ "screenshot_path": str(final_path),
753
+ "screen_width": screen_width,
754
+ "screen_height": screen_height,
755
+ "image_width": img_width,
756
+ "image_height": img_height,
757
+ "element_count": len(all_som_elements),
758
+ "elements": all_som_elements,
759
+ "popup_detected": popup_bounds is not None,
760
+ "popup_bounds": f"[{popup_bounds[0]},{popup_bounds[1]}][{popup_bounds[2]},{popup_bounds[3]}]" if popup_bounds else None,
761
+ "close_hints": popup_close_hints,
762
+ "message": f"📸 SoM 截图已保存: {final_path}\n"
763
+ f"🏷️ 已标注 {len(all_som_elements)} 个元素({len(som_elements)} 个可点击 + {len(popup_close_hints)} 个X按钮提示)\n"
764
+ f"📋 元素列表:\n{elements_text}{hints_text}\n\n"
765
+ f"💡 使用方法:看图后调用 mobile_click_by_som(编号) 点击对应元素"
766
+ }
767
+
768
+ except ImportError:
769
+ return {"success": False, "message": "❌ 需要安装 Pillow: pip install Pillow"}
770
+ except Exception as e:
771
+ return {"success": False, "message": f"❌ SoM 截图失败: {e}"}
772
+
773
+ def click_by_som(self, index: int) -> Dict:
774
+ """根据 SoM 编号点击元素
775
+
776
+ 配合 take_screenshot_with_som 使用。
777
+ 看图后直接说"点击 3 号",调用此函数即可。
778
+
779
+ Args:
780
+ index: 元素编号(从 1 开始)
781
+
782
+ Returns:
783
+ 点击结果
784
+ """
785
+ try:
786
+ if not hasattr(self, '_som_elements') or not self._som_elements:
787
+ return {
788
+ "success": False,
789
+ "message": "❌ 请先调用 mobile_screenshot_with_som 获取元素列表"
790
+ }
791
+
792
+ # 查找对应编号的元素
793
+ target = None
794
+ for elem in self._som_elements:
795
+ if elem['index'] == index:
796
+ target = elem
797
+ break
798
+
799
+ if not target:
800
+ return {
801
+ "success": False,
802
+ "message": f"❌ 未找到编号 {index} 的元素,有效范围: 1-{len(self._som_elements)}"
803
+ }
804
+
805
+ # 点击
806
+ cx, cy = target['center']
807
+ if self._is_ios():
808
+ ios_client = self._get_ios_client()
809
+ if ios_client and hasattr(ios_client, 'wda'):
810
+ ios_client.wda.click(cx, cy)
811
+ else:
812
+ self.client.u2.click(cx, cy)
813
+
814
+ time.sleep(0.3)
815
+
816
+ return {
817
+ "success": True,
818
+ "message": f"✅ 已点击 [{index}] {target['desc']} → ({cx}, {cy})\n💡 建议:再次截图确认操作是否成功",
819
+ "clicked": {
820
+ "index": index,
821
+ "desc": target['desc'],
822
+ "coords": (cx, cy),
823
+ "bounds": target['bounds']
824
+ }
825
+ }
826
+
827
+ except Exception as e:
828
+ return {"success": False, "message": f"❌ 点击失败: {e}\n💡 如果页面已变化,请重新调用 mobile_screenshot_with_som 刷新元素列表"}
829
+
280
830
  def _take_screenshot_no_compress(self, description: str = "") -> Dict:
281
831
  """截图(不压缩,PIL 不可用时的备用方案)"""
282
832
  try:
@@ -649,6 +1199,298 @@ class BasicMobileToolsLite:
649
1199
  except Exception as e:
650
1200
  return {"success": False, "message": f"❌ 点击失败: {e}"}
651
1201
 
1202
+ # ==================== 长按操作 ====================
1203
+
1204
+ def long_press_at_coords(self, x: int, y: int, duration: float = 1.0,
1205
+ image_width: int = 0, image_height: int = 0,
1206
+ crop_offset_x: int = 0, crop_offset_y: int = 0,
1207
+ original_img_width: int = 0, original_img_height: int = 0) -> Dict:
1208
+ """长按坐标(核心功能,支持自动坐标转换)
1209
+
1210
+ Args:
1211
+ x: X 坐标(来自截图分析或屏幕坐标)
1212
+ y: Y 坐标(来自截图分析或屏幕坐标)
1213
+ duration: 长按持续时间(秒),默认 1.0
1214
+ image_width: 压缩后图片宽度(AI 看到的图片尺寸)
1215
+ image_height: 压缩后图片高度(AI 看到的图片尺寸)
1216
+ crop_offset_x: 局部截图的 X 偏移量(局部截图时传入)
1217
+ crop_offset_y: 局部截图的 Y 偏移量(局部截图时传入)
1218
+ original_img_width: 截图原始宽度(压缩前的尺寸,用于精确转换)
1219
+ original_img_height: 截图原始高度(压缩前的尺寸,用于精确转换)
1220
+
1221
+ 坐标转换说明:
1222
+ 1. 全屏压缩截图:AI 坐标 → 原图坐标(基于 image/original_img 比例)
1223
+ 2. 局部裁剪截图:AI 坐标 + 偏移量 = 屏幕坐标
1224
+ """
1225
+ try:
1226
+ # 获取屏幕尺寸
1227
+ screen_width, screen_height = 0, 0
1228
+ if self._is_ios():
1229
+ ios_client = self._get_ios_client()
1230
+ if ios_client and hasattr(ios_client, 'wda'):
1231
+ size = ios_client.wda.window_size()
1232
+ screen_width, screen_height = size[0], size[1]
1233
+ else:
1234
+ return {"success": False, "message": "❌ iOS 客户端未初始化"}
1235
+ else:
1236
+ info = self.client.u2.info
1237
+ screen_width = info.get('displayWidth', 0)
1238
+ screen_height = info.get('displayHeight', 0)
1239
+
1240
+ # 🎯 坐标转换
1241
+ original_x, original_y = x, y
1242
+ converted = False
1243
+ conversion_type = ""
1244
+
1245
+ # 情况1:局部裁剪截图 - 加上偏移量
1246
+ if crop_offset_x > 0 or crop_offset_y > 0:
1247
+ x = x + crop_offset_x
1248
+ y = y + crop_offset_y
1249
+ converted = True
1250
+ conversion_type = "crop_offset"
1251
+ # 情况2:全屏压缩截图 - 按比例转换到原图尺寸
1252
+ elif image_width > 0 and image_height > 0:
1253
+ target_width = original_img_width if original_img_width > 0 else screen_width
1254
+ target_height = original_img_height if original_img_height > 0 else screen_height
1255
+
1256
+ if target_width > 0 and target_height > 0:
1257
+ if image_width != target_width or image_height != target_height:
1258
+ x = int(x * target_width / image_width)
1259
+ y = int(y * target_height / image_height)
1260
+ converted = True
1261
+ conversion_type = "scale"
1262
+
1263
+ # 执行长按
1264
+ if self._is_ios():
1265
+ ios_client = self._get_ios_client()
1266
+ # iOS 使用 tap_hold 或 swipe 原地实现长按
1267
+ if hasattr(ios_client.wda, 'tap_hold'):
1268
+ ios_client.wda.tap_hold(x, y, duration=duration)
1269
+ else:
1270
+ # 兜底:用原地 swipe 模拟长按
1271
+ ios_client.wda.swipe(x, y, x, y, duration=duration)
1272
+ else:
1273
+ self.client.u2.long_click(x, y, duration=duration)
1274
+
1275
+ time.sleep(0.3)
1276
+
1277
+ # 计算百分比坐标(用于跨设备兼容)
1278
+ x_percent = round(x / screen_width * 100, 1) if screen_width > 0 else 0
1279
+ y_percent = round(y / screen_height * 100, 1) if screen_height > 0 else 0
1280
+
1281
+ # 记录操作
1282
+ self._record_operation(
1283
+ 'long_press',
1284
+ x=x,
1285
+ y=y,
1286
+ x_percent=x_percent,
1287
+ y_percent=y_percent,
1288
+ duration=duration,
1289
+ screen_width=screen_width,
1290
+ screen_height=screen_height,
1291
+ ref=f"coords_{x}_{y}"
1292
+ )
1293
+
1294
+ if converted:
1295
+ if conversion_type == "crop_offset":
1296
+ return {
1297
+ "success": True,
1298
+ "message": f"✅ 长按成功: ({x}, {y}) 持续 {duration}s\n"
1299
+ f" 🔍 局部截图坐标转换: ({original_x},{original_y}) + 偏移({crop_offset_x},{crop_offset_y}) → ({x},{y})"
1300
+ }
1301
+ else:
1302
+ return {
1303
+ "success": True,
1304
+ "message": f"✅ 长按成功: ({x}, {y}) 持续 {duration}s\n"
1305
+ f" 📐 坐标已转换: ({original_x},{original_y}) → ({x},{y})\n"
1306
+ f" 🖼️ 图片尺寸: {image_width}x{image_height} → 屏幕: {screen_width}x{screen_height}"
1307
+ }
1308
+ else:
1309
+ return {
1310
+ "success": True,
1311
+ "message": f"✅ 长按成功: ({x}, {y}) 持续 {duration}s [相对位置: {x_percent}%, {y_percent}%]"
1312
+ }
1313
+ except Exception as e:
1314
+ return {"success": False, "message": f"❌ 长按失败: {e}"}
1315
+
1316
+ def long_press_by_percent(self, x_percent: float, y_percent: float, duration: float = 1.0) -> Dict:
1317
+ """通过百分比坐标长按(跨设备兼容)
1318
+
1319
+ 百分比坐标原理:
1320
+ - 屏幕左上角是 (0%, 0%),右下角是 (100%, 100%)
1321
+ - 屏幕正中央是 (50%, 50%)
1322
+ - 像素坐标 = 屏幕尺寸 × (百分比 / 100)
1323
+
1324
+ Args:
1325
+ x_percent: X轴百分比 (0-100),0=最左,50=中间,100=最右
1326
+ y_percent: Y轴百分比 (0-100),0=最上,50=中间,100=最下
1327
+ duration: 长按持续时间(秒),默认 1.0
1328
+
1329
+ 优势:
1330
+ - 同样的百分比在不同分辨率设备上都能点到相同相对位置
1331
+ - 录制一次,多设备回放
1332
+ """
1333
+ try:
1334
+ # 第1步:获取屏幕尺寸
1335
+ if self._is_ios():
1336
+ ios_client = self._get_ios_client()
1337
+ if ios_client and hasattr(ios_client, 'wda'):
1338
+ size = ios_client.wda.window_size()
1339
+ width, height = size[0], size[1]
1340
+ else:
1341
+ return {"success": False, "message": "❌ iOS 客户端未初始化"}
1342
+ else:
1343
+ info = self.client.u2.info
1344
+ width = info.get('displayWidth', 0)
1345
+ height = info.get('displayHeight', 0)
1346
+
1347
+ if width == 0 or height == 0:
1348
+ return {"success": False, "message": "❌ 无法获取屏幕尺寸"}
1349
+
1350
+ # 第2步:百分比转像素坐标
1351
+ x = int(width * x_percent / 100)
1352
+ y = int(height * y_percent / 100)
1353
+
1354
+ # 第3步:执行长按
1355
+ if self._is_ios():
1356
+ ios_client = self._get_ios_client()
1357
+ if hasattr(ios_client.wda, 'tap_hold'):
1358
+ ios_client.wda.tap_hold(x, y, duration=duration)
1359
+ else:
1360
+ ios_client.wda.swipe(x, y, x, y, duration=duration)
1361
+ else:
1362
+ self.client.u2.long_click(x, y, duration=duration)
1363
+
1364
+ time.sleep(0.3)
1365
+
1366
+ # 第4步:记录操作
1367
+ self._record_operation(
1368
+ 'long_press',
1369
+ x=x,
1370
+ y=y,
1371
+ x_percent=x_percent,
1372
+ y_percent=y_percent,
1373
+ duration=duration,
1374
+ screen_width=width,
1375
+ screen_height=height,
1376
+ ref=f"percent_{x_percent}_{y_percent}"
1377
+ )
1378
+
1379
+ return {
1380
+ "success": True,
1381
+ "message": f"✅ 百分比长按成功: ({x_percent}%, {y_percent}%) → 像素({x}, {y}) 持续 {duration}s",
1382
+ "screen_size": {"width": width, "height": height},
1383
+ "percent": {"x": x_percent, "y": y_percent},
1384
+ "pixel": {"x": x, "y": y},
1385
+ "duration": duration
1386
+ }
1387
+ except Exception as e:
1388
+ return {"success": False, "message": f"❌ 百分比长按失败: {e}"}
1389
+
1390
+ def long_press_by_text(self, text: str, duration: float = 1.0) -> Dict:
1391
+ """通过文本长按
1392
+
1393
+ Args:
1394
+ text: 元素的文本内容(精确匹配)
1395
+ duration: 长按持续时间(秒),默认 1.0
1396
+ """
1397
+ try:
1398
+ if self._is_ios():
1399
+ ios_client = self._get_ios_client()
1400
+ if ios_client and hasattr(ios_client, 'wda'):
1401
+ elem = ios_client.wda(name=text)
1402
+ if not elem.exists:
1403
+ elem = ios_client.wda(label=text)
1404
+ if elem.exists:
1405
+ # iOS 元素长按
1406
+ bounds = elem.bounds
1407
+ x = int((bounds.x + bounds.x + bounds.width) / 2)
1408
+ y = int((bounds.y + bounds.y + bounds.height) / 2)
1409
+ if hasattr(ios_client.wda, 'tap_hold'):
1410
+ ios_client.wda.tap_hold(x, y, duration=duration)
1411
+ else:
1412
+ ios_client.wda.swipe(x, y, x, y, duration=duration)
1413
+ time.sleep(0.3)
1414
+ self._record_operation('long_press', element=text, duration=duration, ref=text)
1415
+ return {"success": True, "message": f"✅ 长按成功: '{text}' 持续 {duration}s"}
1416
+ return {"success": False, "message": f"❌ 文本不存在: {text}"}
1417
+ else:
1418
+ # 先查 XML 树,找到元素
1419
+ found_elem = self._find_element_in_tree(text)
1420
+
1421
+ if found_elem:
1422
+ attr_type = found_elem['attr_type']
1423
+ attr_value = found_elem['attr_value']
1424
+ bounds = found_elem.get('bounds')
1425
+
1426
+ # 根据找到的属性类型,使用对应的选择器
1427
+ if attr_type == 'text':
1428
+ elem = self.client.u2(text=attr_value)
1429
+ elif attr_type == 'textContains':
1430
+ elem = self.client.u2(textContains=attr_value)
1431
+ elif attr_type == 'description':
1432
+ elem = self.client.u2(description=attr_value)
1433
+ elif attr_type == 'descriptionContains':
1434
+ elem = self.client.u2(descriptionContains=attr_value)
1435
+ else:
1436
+ elem = None
1437
+
1438
+ if elem and elem.exists(timeout=1):
1439
+ elem.long_click(duration=duration)
1440
+ time.sleep(0.3)
1441
+ self._record_operation('long_press', element=text, duration=duration, ref=f"{attr_type}:{attr_value}")
1442
+ return {"success": True, "message": f"✅ 长按成功({attr_type}): '{text}' 持续 {duration}s"}
1443
+
1444
+ # 如果选择器失败,用坐标兜底
1445
+ if bounds:
1446
+ x = (bounds[0] + bounds[2]) // 2
1447
+ y = (bounds[1] + bounds[3]) // 2
1448
+ self.client.u2.long_click(x, y, duration=duration)
1449
+ time.sleep(0.3)
1450
+ self._record_operation('long_press', element=text, x=x, y=y, duration=duration, ref=f"coords:{x},{y}")
1451
+ return {"success": True, "message": f"✅ 长按成功(坐标兜底): '{text}' @ ({x},{y}) 持续 {duration}s"}
1452
+
1453
+ return {"success": False, "message": f"❌ 文本不存在: {text}"}
1454
+ except Exception as e:
1455
+ return {"success": False, "message": f"❌ 长按失败: {e}"}
1456
+
1457
+ def long_press_by_id(self, resource_id: str, duration: float = 1.0) -> Dict:
1458
+ """通过 resource-id 长按
1459
+
1460
+ Args:
1461
+ resource_id: 元素的 resource-id
1462
+ duration: 长按持续时间(秒),默认 1.0
1463
+ """
1464
+ try:
1465
+ if self._is_ios():
1466
+ ios_client = self._get_ios_client()
1467
+ if ios_client and hasattr(ios_client, 'wda'):
1468
+ elem = ios_client.wda(id=resource_id)
1469
+ if not elem.exists:
1470
+ elem = ios_client.wda(name=resource_id)
1471
+ if elem.exists:
1472
+ bounds = elem.bounds
1473
+ x = int((bounds.x + bounds.x + bounds.width) / 2)
1474
+ y = int((bounds.y + bounds.y + bounds.height) / 2)
1475
+ if hasattr(ios_client.wda, 'tap_hold'):
1476
+ ios_client.wda.tap_hold(x, y, duration=duration)
1477
+ else:
1478
+ ios_client.wda.swipe(x, y, x, y, duration=duration)
1479
+ time.sleep(0.3)
1480
+ self._record_operation('long_press', element=resource_id, duration=duration, ref=resource_id)
1481
+ return {"success": True, "message": f"✅ 长按成功: {resource_id} 持续 {duration}s"}
1482
+ return {"success": False, "message": f"❌ 元素不存在: {resource_id}"}
1483
+ else:
1484
+ elem = self.client.u2(resourceId=resource_id)
1485
+ if elem.exists(timeout=0.5):
1486
+ elem.long_click(duration=duration)
1487
+ time.sleep(0.3)
1488
+ self._record_operation('long_press', element=resource_id, duration=duration, ref=resource_id)
1489
+ return {"success": True, "message": f"✅ 长按成功: {resource_id} 持续 {duration}s"}
1490
+ return {"success": False, "message": f"❌ 元素不存在: {resource_id}"}
1491
+ except Exception as e:
1492
+ return {"success": False, "message": f"❌ 长按失败: {e}"}
1493
+
652
1494
  # ==================== 输入操作 ====================
653
1495
 
654
1496
  def input_text_by_id(self, resource_id: str, text: str) -> Dict:
@@ -1374,21 +2216,53 @@ class BasicMobileToolsLite:
1374
2216
  pass
1375
2217
 
1376
2218
  if not close_candidates:
1377
- # 控件树未找到,自动截全屏图供 AI 分析
1378
- screenshot_result = self.take_screenshot(description="弹窗全屏", compress=True)
1379
-
1380
- # 构建更详细的视觉分析提示
1381
- visual_hint = "请仔细查看截图,找到关闭按钮(通常是 × 或 X 图标)。"
2219
+ # 如果检测到弹窗区域,先尝试点击常见的关闭按钮位置
1382
2220
  if popup_bounds:
1383
2221
  px1, py1, px2, py2 = popup_bounds
1384
- visual_hint += f" 弹窗区域大约在 [{px1},{py1}] 到 [{px2},{py2}],关闭按钮通常在弹窗的右上角或正上方。"
1385
- else:
1386
- visual_hint += " 关闭按钮通常在屏幕右上角、弹窗右上角、或弹窗下方中间位置。"
2222
+ popup_width = px2 - px1
2223
+ popup_height = py2 - py1
2224
+
2225
+ # 【优化】X按钮有三种常见位置:
2226
+ # 1. 弹窗内靠近顶部边界(内嵌X按钮)- 最常见
2227
+ # 2. 弹窗边界上方(浮动X按钮)
2228
+ # 3. 弹窗正下方(底部关闭按钮)
2229
+ offset_x = max(60, int(popup_width * 0.07)) # 宽度7%
2230
+ offset_y_above = max(35, int(popup_height * 0.025)) # 高度2.5%,在边界之上
2231
+ offset_y_near = max(45, int(popup_height * 0.03)) # 高度3%,紧贴顶边界内侧
2232
+
2233
+ try_positions = [
2234
+ # 【最高优先级】弹窗内紧贴顶部边界
2235
+ (px2 - offset_x, py1 + offset_y_near, "弹窗右上角"),
2236
+ # 弹窗边界上方(浮动X按钮)
2237
+ (px2 - offset_x, py1 - offset_y_above, "弹窗右上浮"),
2238
+ # 弹窗正下方中间(底部关闭按钮)
2239
+ ((px1 + px2) // 2, py2 + max(50, int(popup_height * 0.04)), "弹窗下方中间"),
2240
+ # 弹窗正上方中间
2241
+ ((px1 + px2) // 2, py1 - 40, "弹窗正上方"),
2242
+ ]
2243
+
2244
+ for try_x, try_y, position_name in try_positions:
2245
+ if 0 <= try_x <= screen_width and 0 <= try_y <= screen_height:
2246
+ self.client.u2.click(try_x, try_y)
2247
+ time.sleep(0.3)
2248
+
2249
+ # 尝试后截图,让 AI 判断是否成功
2250
+ screenshot_result = self.take_screenshot("尝试关闭后")
2251
+ return {
2252
+ "success": True,
2253
+ "message": f"✅ 已尝试点击常见关闭按钮位置",
2254
+ "tried_positions": [p[2] for p in try_positions],
2255
+ "screenshot": screenshot_result.get("screenshot_path", ""),
2256
+ "tip": "请查看截图确认弹窗是否已关闭。如果还在,可手动分析截图找到关闭按钮位置。"
2257
+ }
2258
+
2259
+ # 没有检测到弹窗区域,截图让 AI 分析
2260
+ screenshot_result = self.take_screenshot(description="页面截图", compress=True)
1387
2261
 
1388
2262
  return {
1389
2263
  "success": False,
1390
- "message": "❌ 控件树未找到关闭按钮,已截全屏图供 AI 视觉分析",
1391
- "action_required": visual_hint + " 找到后调用 mobile_click_at_coords(x, y, image_width, image_height, original_img_width, original_img_height) 点击。",
2264
+ "message": "❌ 未检测到弹窗区域,已截图供 AI 分析",
2265
+ "action_required": "请查看截图找到关闭按钮,调用 mobile_click_at_coords 点击",
1392
2266
  "screenshot": screenshot_result.get("screenshot_path", ""),
1393
2267
  "screen_size": {"width": screen_width, "height": screen_height},
1394
2268
  "image_size": {
@@ -1399,16 +2273,8 @@ class BasicMobileToolsLite:
1399
2273
  "width": screenshot_result.get("original_img_width", screen_width),
1400
2274
  "height": screenshot_result.get("original_img_height", screen_height)
1401
2275
  },
1402
- "popup_detected": popup_bounds is not None,
1403
- "popup_bounds": f"[{popup_bounds[0]},{popup_bounds[1]}][{popup_bounds[2]},{popup_bounds[3]}]" if popup_bounds else None,
1404
- "search_areas": [
1405
- "弹窗右上角(最常见)",
1406
- "弹窗正上方外侧(浮动X按钮)",
1407
- "弹窗下方中间(某些广告)",
1408
- "屏幕右上角"
1409
- ],
1410
- "button_features": "关闭按钮通常是:小圆形/方形图标、灰色或白色、带有 × 或 X 符号",
1411
- "tip": "注意:不要点击广告内容区域,只点击关闭按钮"
2276
+ "search_areas": ["弹窗右上角", "弹窗正上方", "弹窗下方中间", "屏幕右上角"],
2277
+ "time_warning": "⚠️ 截图分析期间弹窗可能自动消失。如果是定时弹窗,建议等待其自动消失。"
1412
2278
  }
1413
2279
 
1414
2280
  # 按得分排序,取最可能的
@@ -1632,6 +2498,22 @@ class BasicMobileToolsLite:
1632
2498
  " return True",
1633
2499
  "",
1634
2500
  "",
2501
+ "def long_press_by_percent(d, x_percent, y_percent, duration=1.0):",
2502
+ ' """',
2503
+ ' 百分比长按(跨分辨率兼容)',
2504
+ ' ',
2505
+ ' 原理:屏幕左上角 (0%, 0%),右下角 (100%, 100%)',
2506
+ ' 优势:同样的百分比在不同分辨率设备上都能长按到相同相对位置',
2507
+ ' """',
2508
+ " info = d.info",
2509
+ " width = info.get('displayWidth', 0)",
2510
+ " height = info.get('displayHeight', 0)",
2511
+ " x = int(width * x_percent / 100)",
2512
+ " y = int(height * y_percent / 100)",
2513
+ " d.long_click(x, y, duration=duration)",
2514
+ " return True",
2515
+ "",
2516
+ "",
1635
2517
  "def test_main():",
1636
2518
  " # 连接设备",
1637
2519
  " d = u2.connect()",
@@ -1737,6 +2619,48 @@ class BasicMobileToolsLite:
1737
2619
  script_lines.append(" time.sleep(0.5)")
1738
2620
  script_lines.append(" ")
1739
2621
 
2622
+ elif action == 'long_press':
2623
+ ref = op.get('ref', '')
2624
+ element = op.get('element', '')
2625
+ duration = op.get('duration', 1.0)
2626
+ has_coords = 'x' in op and 'y' in op
2627
+ has_percent = 'x_percent' in op and 'y_percent' in op
2628
+
2629
+ # 判断 ref 是否为坐标格式
2630
+ is_coords_ref = ref.startswith('coords_') or ref.startswith('coords:')
2631
+ is_percent_ref = ref.startswith('percent_')
2632
+
2633
+ # 优先级:ID > 文本 > 百分比 > 坐标
2634
+ if ref and (':id/' in ref or ref.startswith('com.')):
2635
+ # 使用 resource-id
2636
+ script_lines.append(f" # 步骤{step_num}: 长按元素 (ID定位,最稳定)")
2637
+ script_lines.append(f" d(resourceId='{ref}').long_click(duration={duration})")
2638
+ elif ref and not is_coords_ref and not is_percent_ref and ':' not in ref:
2639
+ # 使用文本
2640
+ script_lines.append(f" # 步骤{step_num}: 长按文本 '{ref}' (文本定位)")
2641
+ script_lines.append(f" d(text='{ref}').long_click(duration={duration})")
2642
+ elif ref and ':' in ref and not is_coords_ref and not is_percent_ref:
2643
+ actual_text = ref.split(':', 1)[1] if ':' in ref else ref
2644
+ script_lines.append(f" # 步骤{step_num}: 长按文本 '{actual_text}' (文本定位)")
2645
+ script_lines.append(f" d(text='{actual_text}').long_click(duration={duration})")
2646
+ elif has_percent:
2647
+ # 使用百分比
2648
+ x_pct = op['x_percent']
2649
+ y_pct = op['y_percent']
2650
+ desc = f" ({element})" if element else ""
2651
+ script_lines.append(f" # 步骤{step_num}: 长按位置{desc} (百分比定位,跨分辨率兼容)")
2652
+ script_lines.append(f" long_press_by_percent(d, {x_pct}, {y_pct}, duration={duration}) # 原坐标: ({op.get('x', '?')}, {op.get('y', '?')})")
2653
+ elif has_coords:
2654
+ # 坐标兜底
2655
+ desc = f" ({element})" if element else ""
2656
+ script_lines.append(f" # 步骤{step_num}: 长按坐标{desc} (⚠️ 坐标定位,可能不兼容其他分辨率)")
2657
+ script_lines.append(f" d.long_click({op['x']}, {op['y']}, duration={duration})")
2658
+ else:
2659
+ continue
2660
+
2661
+ script_lines.append(" time.sleep(0.5) # 等待响应")
2662
+ script_lines.append(" ")
2663
+
1740
2664
  elif action == 'swipe':
1741
2665
  direction = op.get('direction', 'up')
1742
2666
  script_lines.append(f" # 步骤{step_num}: 滑动 {direction}")