连接到新窗口,当您不知道其名称时,应如何操作?

huangapple go评论71阅读模式
英文:

(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.

huangapple
  • 本文由 发表于 2023年4月17日 11:16:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定