英文:
(Pywinauto) How to connect to new window when you dont know its name
问题
I'm doing automation for a chat application on Windows using the Pywinauto module in Python. When I create a new chat room, it generates a new window with an unfixed window name. The window name depends on the number of chat room members. For example, if there are two members, the chat room name is displayed as the names of those two members. But if there are more than 5 members, the chat room name will be shown as "member x and 4 others."
So, the window name is always changing, and I didn't know how to connect to the new window to perform actions.
I'm using Desktop(backend='uia'). Backend win32 is also too.
(SOLVED)
I found a solution for this workaround:
Step 1: Connect to the main window (app = Application(backend='uia').connect(title=mainwindowapp))
Step 2: Get the top window (app.top_window())
英文:
Im doing automation for a chat application on windows by module Pywinauto in python. When i create a new chat room, it generated a new window with unfixed name of window. The window name depends on numbers of chat room members. Example : if there are two members, chat room name displayed as name of that two members. But if there are more than 5 members, chat room name will be showns as member x and 4 others.
So the windows name is always changed. I dont know how to connect the new window to perform action
Im using Desktop(backend ='uia')
Backend win32 is also too
(SOLVED)
I walkaround and found a solution for this.
Step1: connect to mainwindow (app = Application(backend ='uia').connect(title = mainwindowapp)
Step2: get top window ( app.top_window() )
答案1
得分: 2
如果这个新窗口在同一个进程中,最好使用Application(backend="uia").connect(process=pid)
,您可以通过方法.process_id()
从主窗口获取pid
。然后,您可以获取该进程的所有顶级窗口:
from pywinauto import Desktop, Application
pid = Desktop(backend="uia").window(title="Main Window Title").process_id()
app = Application(backend="uia").connect(process=pid)
top_windows_before = app.windows()
# 进行一些操作(创建新的聊天)
for w in app.windows():
if w not in top_windows_before:
new_chat = w
包装对象之间的比较应该有效,这就是为什么我建议使用布尔运算符w not in top_windows_before
。
英文:
If this new window is in the same process, it's better to use Application(backend="uia").connect(process=pid)
where you can get pid
from main window by method .process_id()
. Then you can get all top level windows of this process:
from pywinauto import Desktop, Application
pid = Desktop(backend="uia").window(title="Main Window Title").process_id()
app = Application(backend="uia").connect(process=pid)
top_windows_before = app.windows()
# do some actions (create new chat)
for w in app.windows():
if w not in top_windows_before:
new_chat = w
Comparison between wrapper objects should work, that's why I suggest boolean operator w not in top_windows_before
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论