forked from Python51888/Midscene-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_usage.py
More file actions
120 lines (87 loc) · 3.41 KB
/
basic_usage.py
File metadata and controls
120 lines (87 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
Basic usage examples for Midscene Python
"""
import asyncio
from midscene import Agent
from midscene.web import SeleniumWebPage
from midscene.android import AndroidAgent
async def web_automation_example():
"""Basic web automation example"""
print("🌐 Web Automation Example")
# Create web page instance
with SeleniumWebPage.create(headless=False) as page:
# Create agent
agent = Agent(page)
# Navigate to website
await page.navigate_to("https://example.com")
# Use AI to interact with the page
await agent.ai_action("点击登录按钮")
await agent.ai_action("在用户名输入框输入 'demo@example.com'")
await agent.ai_action("在密码输入框输入 'password123'")
await agent.ai_action("点击提交按钮")
# Extract data using AI
user_info = await agent.ai_extract({
"username": "用户名",
"email": "邮箱地址",
"last_login": "最后登录时间"
})
print(f"提取的用户信息: {user_info}")
# Assert page state
await agent.ai_assert("页面显示欢迎信息")
print("✅ Web automation completed successfully!")
async def android_automation_example():
"""Basic Android automation example"""
print("📱 Android Automation Example")
try:
# Create Android agent
agent = await AndroidAgent.create()
# Launch app
await agent.launch_app("com.android.settings")
# Use AI to navigate
await agent.ai_action("点击WLAN设置")
await agent.ai_action("滑动到底部")
# Extract information
wifi_list = await agent.ai_extract({
"available_networks": [
{"name": "网络名称", "security": "安全类型", "signal": "信号强度"}
]
})
print(f"可用WiFi网络: {wifi_list}")
# Go back
await agent.back()
print("✅ Android automation completed successfully!")
except Exception as e:
print(f"❌ Android automation failed: {e}")
async def playwright_example():
"""Playwright integration example"""
print("🎭 Playwright Example")
from midscene.web import PlaywrightWebPage
# Create Playwright page
async with await PlaywrightWebPage.create(headless=False) as page:
agent = Agent(page)
# Navigate and interact
await page.navigate_to("https://playwright.dev")
# Use AI for navigation
await agent.ai_action("点击文档链接")
await agent.ai_action("搜索 'getting started'")
# Extract page information
page_info = await agent.ai_extract({
"title": "页面标题",
"description": "页面描述",
"sections": ["主要章节列表"]
})
print(f"页面信息: {page_info}")
print("✅ Playwright example completed!")
async def main():
"""Run all examples"""
print("🚀 Midscene Python Examples\n")
# Web automation with Selenium
await web_automation_example()
print()
# Playwright example
await playwright_example()
print()
# Android automation (if device available)
await android_automation_example()
if __name__ == "__main__":
asyncio.run(main())