|
1 | 1 | # server.py |
2 | 2 | from mcp.server.fastmcp import FastMCP |
3 | 3 | import uiautomator2 as u2 |
4 | | -from typing import List, Optional, Dict, Any, Tuple |
| 4 | +from typing import List, Optional, Dict, Any |
5 | 5 | import shutil |
6 | 6 | import subprocess |
7 | 7 | import asyncio |
8 | | -from dataclasses import dataclass |
9 | 8 | from typing import TypedDict |
10 | 9 |
|
11 | 10 | # Create an MCP server |
@@ -358,7 +357,7 @@ def click( |
358 | 357 | else: |
359 | 358 | raise ValueError(f"Invalid selector_type: {selector_type}") |
360 | 359 |
|
361 | | - if el: |
| 360 | + if el and el.exists: |
362 | 361 | el.click() |
363 | 362 | return True |
364 | 363 | return False |
@@ -423,7 +422,7 @@ def get_element_info( |
423 | 422 | else: |
424 | 423 | raise ValueError(f"Invalid selector_type: {selector_type}") |
425 | 424 |
|
426 | | - if el: |
| 425 | + if el and el.exists: |
427 | 426 | info = el.info |
428 | 427 | return { |
429 | 428 | "text": info.get("text", ""), |
@@ -500,7 +499,8 @@ def wait_for_element( |
500 | 499 | elif selector_type == "resourceId": |
501 | 500 | return d(resourceId=selector).wait(timeout=timeout) |
502 | 501 | elif selector_type == "description": |
503 | | - return d(description=selector).wait(timeout=timeout) |
| 502 | + el = d(description=selector).wait(timeout=timeout) |
| 503 | + return el is not None and el.exists |
504 | 504 | else: |
505 | 505 | raise ValueError(f"Invalid selector_type: {selector_type}") |
506 | 506 | except Exception as e: |
@@ -559,7 +559,7 @@ def long_click( |
559 | 559 | else: |
560 | 560 | raise ValueError(f"Invalid selector_type: {selector_type}") |
561 | 561 |
|
562 | | - if el.exists: |
| 562 | + if el and el.exists: |
563 | 563 | el.long_click(duration=duration) |
564 | 564 | return True |
565 | 565 | return False |
@@ -589,7 +589,8 @@ def scroll_to( |
589 | 589 | elif selector_type == "resourceId": |
590 | 590 | return d(scrollable=True).scroll.to(resourceId=selector) |
591 | 591 | elif selector_type == "description": |
592 | | - return d(scrollable=True).scroll.to(description=selector) |
| 592 | + el = d(scrollable=True).scroll.to(description=selector) |
| 593 | + return el is not None and el.exists |
593 | 594 | else: |
594 | 595 | raise ValueError(f"Invalid selector_type: {selector_type}") |
595 | 596 | except Exception as e: |
@@ -628,7 +629,7 @@ def drag( |
628 | 629 | else: |
629 | 630 | raise ValueError(f"Invalid selector_type: {selector_type}") |
630 | 631 |
|
631 | | - if el.exists: |
| 632 | + if el and el.exists: |
632 | 633 | el.drag_to(to_x, to_y) |
633 | 634 | return True |
634 | 635 | return False |
|
0 commit comments