-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_wechat_send_fixed.py
More file actions
189 lines (153 loc) · 5.76 KB
/
test_wechat_send_fixed.py
File metadata and controls
189 lines (153 loc) · 5.76 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
"""
WeChat UI Automation Test - Windows Only (Fixed Coordinates Version)
Test script for sending WeChat messages using fixed screen coordinates
Usage:
python test_wechat_send_fixed.py "群名称" "测试消息"
python test_wechat_send_fixed.py "文件传输助手" "Hello World"
Requirements:
pip install uiautomation pyautogui
WeChat for PC must be logged in and running
WeChat window should be in normal state (not maximized/minimized to tray)
Note:
Run this script on Windows with WeChat open
"""
import sys
import time
import argparse
def check_dependencies():
"""Check if required packages are installed"""
try:
import uiautomation as auto
except ImportError:
print("[ERROR] uiautomation not installed!")
print("[INFO] Please run: pip install uiautomation")
sys.exit(1)
try:
import pyautogui
except ImportError:
print("[ERROR] pyautogui not installed!")
print("[INFO] Please run: pip install pyautogui")
sys.exit(1)
return auto, pyautogui
def find_wechat_window(auto):
"""Find WeChat window and return its position"""
print("[INFO] Looking for WeChat window...")
try:
wx = auto.WindowControl(ClassName="WeChatMainWndForPC")
if wx.Exists(1):
rect = wx.BoundingRectangle
print(f"[OK] Found WeChat window: {rect}")
return rect
except Exception as e:
print(f"[ERROR] {e}")
return None
def send_message(auto, pyautogui, target, message, at_all=False):
"""Send message using fixed coordinates"""
try:
print(f"[INFO] Preparing to send message to [{target}]...")
# 1. Find WeChat window position
wx_rect = find_wechat_window(auto)
if not wx_rect:
print("[ERROR] Cannot find WeChat window!")
return False
# Calculate fixed coordinates based on window position
# WeChat input box is typically at the bottom of the window
# Assuming standard WeChat window size/layout
input_x = (wx_rect.left + wx_rect.right) // 2 # Center horizontally
input_y = wx_rect.bottom - 60 # 60px from bottom (input box area)
print(f"[INFO] Calculated input box position: ({input_x}, {input_y})")
# 2. Activate WeChat window
print("[INFO] Activating WeChat window...")
wx = auto.WindowControl(ClassName="WeChatMainWndForPC")
if wx.Exists(1):
try:
wx.SwitchToThisWindow()
except:
try:
wx.SetFocus()
except:
pass
time.sleep(0.5)
# 3. Open search with Ctrl+F
print("[INFO] Opening search...")
pyautogui.keyDown('ctrl')
pyautogui.keyDown('f')
pyautogui.keyUp('f')
pyautogui.keyUp('ctrl')
time.sleep(0.8)
# 4. Type target name
print(f"[INFO] Searching for [{target}]...")
pyautogui.typewrite(target, interval=0.01)
time.sleep(1.0)
# 5. Press Enter to open chat
print("[INFO] Opening chat...")
pyautogui.keyDown('return')
pyautogui.keyUp('return')
time.sleep(1.5) # Wait for chat to load
# 6. Click on input box (fixed position)
print(f"[INFO] Clicking input box at ({input_x}, {input_y})...")
pyautogui.click(input_x, input_y)
time.sleep(0.5)
# 7. Handle @所有人 if needed
if at_all:
print("[INFO] Adding @所有人...")
pyautogui.typewrite("@所有人", interval=0.01)
time.sleep(0.6)
pyautogui.keyDown('return')
pyautogui.keyUp('return')
time.sleep(0.3)
# 8. Type message
print(f"[INFO] Typing message...")
pyautogui.typewrite(message, interval=0.01)
time.sleep(0.5)
# 9. Send message
print("[INFO] Sending message...")
pyautogui.keyDown('return')
pyautogui.keyUp('return')
time.sleep(0.5)
print(f"[OK] Message sent to [{target}] successfully!")
return True
except Exception as e:
print(f"[ERROR] Failed to send message: {e}")
import traceback
traceback.print_exc()
return False
def main():
parser = argparse.ArgumentParser(
description='Test WeChat UI Automation - Fixed Coordinates Version'
)
parser.add_argument('target', help='Target contact or group name')
parser.add_argument('message', help='Message content to send')
parser.add_argument('--at-all', action='store_true', help='Add @所有人')
parser.add_argument('--delay', type=float, default=3.0, help='Delay after sending')
args = parser.parse_args()
print("="*60)
print("WeChat UI Automation Test - Fixed Coordinates")
print("="*60)
print(f"[CONFIG] Target: {args.target}")
print(f"[CONFIG] Message: {args.message[:50]}...")
print(f"[CONFIG] @所有人: {args.at_all}")
print("="*60)
# Check dependencies
auto, pyautogui = check_dependencies()
# Configure pyautogui
pyautogui.PAUSE = 0.1
pyautogui.FAILSAFE = True # Move mouse to top-left corner to abort
# Send message
success = send_message(auto, pyautogui, args.target, args.message, args.at_all)
if success:
print("="*60)
print("[SUCCESS] Test completed!")
print("="*60)
if args.delay > 0:
print(f"[INFO] Waiting {args.delay}s...")
time.sleep(args.delay)
sys.exit(0)
else:
print("="*60)
print("[FAILED] Test failed!")
print("="*60)
sys.exit(1)
if __name__ == "__main__":
main()