-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinspect_window.py
More file actions
278 lines (208 loc) · 7.85 KB
/
inspect_window.py
File metadata and controls
278 lines (208 loc) · 7.85 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""
检查窗口结构
支持:
1. Win32 窗口层级结构
2. UI Automation 树结构
3. Chrome DevTools Protocol (CDP) - 用于 Electron 应用
"""
import win32gui
import win32api
import win32con
import time
import json
def get_window_tree(hwnd, level=0, max_level=5):
"""获取窗口的层级结构"""
if level > max_level:
return None
try:
class_name = win32gui.GetClassName(hwnd)
text = win32gui.GetWindowText(hwnd)
rect = win32gui.GetWindowRect(hwnd)
visible = win32gui.IsWindowVisible(hwnd)
enabled = win32gui.IsWindowEnabled(hwnd)
node = {
"hwnd": hwnd,
"class": class_name,
"text": text,
"rect": rect,
"visible": visible,
"enabled": enabled,
"children": []
}
# 获取子窗口
def enum_child(child_hwnd, results):
child_node = get_window_tree(child_hwnd, level + 1, max_level)
if child_node:
results.append(child_node)
return True
children = []
win32gui.EnumChildWindows(hwnd, enum_child, children)
node["children"] = children
return node
except Exception as e:
return None
def print_window_tree(node, level=0, show_invisible=False):
"""打印窗口树"""
if not node:
return
if not show_invisible and not node["visible"]:
return
indent = " " * level
# 显示信息
info = f"{indent}[{level}] "
info += f"句柄:{node['hwnd']:10d} "
info += f"类:{node['class']:30s} "
if node['text']:
info += f"文本:{node['text'][:40]}"
if not node['visible']:
info += " (隐藏)"
if not node['enabled']:
info += " (禁用)"
print(info)
# 递归打印子节点
for child in node["children"]:
print_window_tree(child, level + 1, show_invisible)
def save_window_tree(node, filename="window_tree.json"):
"""保存窗口树到 JSON 文件"""
with open(filename, 'w', encoding='utf-8') as f:
json.dump(node, f, indent=2, ensure_ascii=False)
print(f"💾 窗口树已保存到: {filename}")
def inspect_with_uiautomation():
"""使用 UI Automation 检查窗口"""
try:
from comtypes.client import CreateObject, GetModule
print("\n🔍 使用 UI Automation 检查...")
# 加载 UI Automation
GetModule('UIAutomationCore.dll')
import comtypes.gen.UIAutomationClient as UIA
automation = CreateObject(
'{ff48dba4-60ef-4201-aa87-54103eef594e}',
interface=UIA.IUIAutomation
)
# 获取前台窗口
hwnd = win32gui.GetForegroundWindow()
element = automation.ElementFromHandle(hwnd)
if element:
print(f"✅ UI Automation 元素:")
print(f" 名称: {element.CurrentName}")
print(f" 类名: {element.CurrentClassName}")
print(f" 控件类型: {element.CurrentControlType}")
print(f" 自动化ID: {element.CurrentAutomationId}")
# 遍历子元素
print(f"\n📋 子元素:")
walker = automation.ControlViewWalker
child = walker.GetFirstChildElement(element)
count = 0
while child and count < 20:
try:
name = child.CurrentName
class_name = child.CurrentClassName
control_type = child.CurrentControlType
print(f" [{count}] {name[:40]:40s} 类:{class_name:20s} 类型:{control_type}")
child = walker.GetNextSiblingElement(child)
count += 1
except:
break
return True
except Exception as e:
print(f"⚠️ UI Automation 不可用: {e}")
return False
def inspect_chrome_devtools():
"""
检查 Chrome/Electron 应用的 DOM 结构
需要应用开启了 DevTools Protocol
"""
print("\n🔍 Chrome DevTools Protocol 检查...")
print("💡 提示:需要 Electron 应用开启远程调试")
print(" 启动参数: --remote-debugging-port=9222")
try:
import requests
# 尝试连接到 Chrome DevTools
response = requests.get('http://localhost:9222/json', timeout=2)
if response.status_code == 200:
pages = response.json()
print(f"\n✅ 找到 {len(pages)} 个页面:")
for i, page in enumerate(pages):
print(f"\n [{i}] {page.get('title', 'Untitled')}")
print(f" URL: {page.get('url', 'N/A')}")
print(f" WebSocket: {page.get('webSocketDebuggerUrl', 'N/A')}")
return True
else:
print("❌ 无法连接到 DevTools")
return False
except Exception as e:
print(f"❌ DevTools 不可用: {e}")
print("\n💡 如果是 Electron 应用,可以尝试:")
print(" 1. 在应用中按 Ctrl+Shift+I 打开开发者工具")
print(" 2. 使用开发者工具的 Elements 面板查看 DOM")
return False
def find_kiro_and_inspect():
"""查找 Kiro 窗口并检查"""
print("=" * 60)
print("Kiro 窗口结构检查")
print("=" * 60)
# 查找 Kiro 窗口
print("\n🔍 查找 Kiro 窗口...")
def find_kiro(hwnd, results):
if win32gui.IsWindowVisible(hwnd):
title = win32gui.GetWindowText(hwnd)
if "Kiro" in title:
results.append((hwnd, title))
return True
results = []
win32gui.EnumWindows(find_kiro, results)
if not results:
print("❌ 未找到 Kiro 窗口")
return
hwnd, title = results[0]
print(f"✅ 找到窗口: {title}")
print(f" 句柄: {hwnd}")
# 方法1: Win32 窗口树
print("\n" + "=" * 60)
print("方法1: Win32 窗口层级结构")
print("=" * 60)
tree = get_window_tree(hwnd, max_level=3)
print_window_tree(tree)
# 保存到文件
save_window_tree(tree, "kiro_window_tree.json")
# 方法2: UI Automation
print("\n" + "=" * 60)
print("方法2: UI Automation")
print("=" * 60)
# 激活窗口
win32gui.SetForegroundWindow(hwnd)
time.sleep(0.5)
inspect_with_uiautomation()
# 方法3: Chrome DevTools
print("\n" + "=" * 60)
print("方法3: Chrome DevTools Protocol")
print("=" * 60)
inspect_chrome_devtools()
# 提示
print("\n" + "=" * 60)
print("💡 查看 DOM 结构的最佳方法:")
print("=" * 60)
print("\n1. 在 Kiro 中按 Ctrl+Shift+I 打开开发者工具")
print("2. 点击 Elements 标签")
print("3. 使用选择工具(左上角箭头)点击元素")
print("4. 在 Elements 面板中查看 HTML 结构")
print("\n5. 在 Console 中可以使用 JavaScript:")
print(" document.querySelector('选择器')")
print(" document.querySelectorAll('选择器')")
print("=" * 60)
if __name__ == "__main__":
import sys
if len(sys.argv) > 1 and sys.argv[1] == "current":
# 检查当前前台窗口
print("⏰ 3 秒后检查当前窗口...")
time.sleep(3)
hwnd = win32gui.GetForegroundWindow()
title = win32gui.GetWindowText(hwnd)
print(f"\n当前窗口: {title}")
print(f"句柄: {hwnd}")
tree = get_window_tree(hwnd, max_level=3)
print_window_tree(tree)
save_window_tree(tree, "current_window_tree.json")
else:
# 检查 Kiro 窗口
find_kiro_and_inspect()