【问题标题】:Connecting to a second Application using pywinauto使用 pywinauto 连接到第二个应用程序
【发布时间】:2017-09-29 13:06:21
【问题描述】:

我正在尝试使用 python 来操作多个应用程序。 目前我能够打开第一个应用程序并使用工具栏打开另一个应用程序。

import pywinauto
import os
os.startfile("Path")
app = pywinauto.application.Application(backend="uia") 
app.connect(path="path")
app.top_window().descendants(control_type="MenuBar")
app_menu = app.top_window().descendants(control_type="MenuBar")[1]
app_menu.items()
appmenu = app.top_window().descendants(control_type="MenuBar")[1]
mi = appmenu.items()[3]
mi.set_focus()
mi2 = app.top_window().descendants(control_type="MenuItem")[1]
mi2.set_focus()
mi2.select()

` 到目前为止,这有效。当我试图控制这个新应用程序时,我得到一个错误。类型错误:connect() 缺少 1 个必需的位置参数:'self' 我用来连接到第二个应用程序的代码:

app2 = pywinauto.application.Application(backend="uia")
app2= pywinauto.application.Application.connect(path="path2")

如何连接到第二个应用程序?

【问题讨论】:

    标签: python python-3.x typeerror pywinauto


    【解决方案1】:

    你有一个错误:

    app2 = pywinauto.application.Application(backend="uia")
    app2= pywinauto.application.Application.connect(path="path2")
    

    应该是:

    app2 = pywinauto.application.Application(backend="uia")
    # Don't forget to start the 'path2' first or use app2.start() instead.
    # Or, well, the first Application()'s automation should start it.
    # Regardless, you would perhaps have to wait some time before app starts and then connect() will work.
    # So put some time.sleep() here or setup a pywinauto's timeout.
    app2.connect(path="path2")
    

    就像您为第一个应用程序所做的那样。

    引发 TypeError 是因为您直接从 Application 类调用 connect(),而不是从 Application() 实例调用。 connect() 方法缺少“self”引用作为第一个参数,当您从实例的指针调用方法时,该参数会自动添加。

    这意味着这将具有相同的效果:

    app2 = pywinauto.application.Application(backend="uia")
    pywinauto.application.Application.connect(app2, path="path2")
    

    看,app2 作为第一个(强制)位置参数传递。这样 pywinauto.application.Application.connect() 知道它应该绑定应用程序的窗口到哪个对象(app2)。如果您将其称为 app2.connect(),则它已经获取了 app2,因此无需传递它。

    【讨论】:

      猜你喜欢
      • 2019-12-16
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 2022-07-09
      • 2017-08-03
      • 2018-06-13
      • 1970-01-01
      相关资源
      最近更新 更多