【问题标题】:uTorrent Automation using pywinauto使用 pywinauto 的 uTorrent 自动化
【发布时间】:2016-09-24 09:02:00
【问题描述】:

我正在尝试使用 pywinauto lib 进行 utorrent 自动化。我想添加一个带有 URL 的种子。此选项位于文件菜单下。我可以打开 uTorrent,然后什么也没有发生。我使用 Swapy 生成此代码。只有当我在 swapy 中运行代码时,才会打开下面的框。但是当我将它保存到文件中并使用 cmd 运行时,只有 utorrent 会打开,并且 cmd 中会发生回溯。

from pywinauto.application import Application

app = Application().Start(cmd_line=u'"C:\\Users\\User\\AppData\\Roaming\\uTorrent\\u Torrent.exe" ')
torrentdfb = app[u'\xb5Torrent4823DF041B09']
torrentdfb.Wait('ready')
menu_item = torrentdfb.MenuItem(u'&File->Add Torrent from &URL...\tCtrl+U')
menu_item.Click()

app.Kill_()

Traceback:
Traceback (most recent call last):
File "AddTorrent.py", line 5, in <module>
torrentdfb.Wait('ready')
File "C:\Python27\lib\site-packages\pywinauto\application.py", line 380, in Wait
WaitUntil(timeout, retry_interval, lambda: self.__check_all_conditions(check_method_names))
File "C:\Python27\lib\site-packages\pywinauto\timings.py", line 308, in WaitUntil
raise err
pywinauto.timings.TimeoutError: timed out

我是 python 编码的新手,我不是专家。如果您提供解释以解决我的问题或代码,将会很有帮助。谢谢!!

【问题讨论】:

    标签: python pywinauto utorrent


    【解决方案1】:

    uTorrent 正在生成另一个进程,这就是我得到它的方式:

    >>> app.windows_()
    []
    >>> app.process
    6096
    >>> app.connect(title_re=u'^μTorrent.*(build \d+).*')
    <pywinauto.application.Application object at 0x000000000405C240>
    >>> app.process
    4044L
    

    这是为我工作的最终代码(使用 32 位 uTorrent 和 32 位 Python 2.7):

    import pywinauto
    
    app = pywinauto.Application().start(r'uTorrent.exe')
    time.sleep(5) # because method connect() has no timeout param yet (planned for 0.6.0)
    app.connect(title_re=u'^\u03bcTorrent.*(build \d+).*')
    
    main_window = app.window_(title_re=u'^\u03bcTorrent.*(build \d+).*')
    main_window.MenuSelect(u'&File->Add Torrent from &URL...\tCtrl+U')
    
    app.AddTorrentFromURL.Edit.SetText('some URL')
    app.AddTorrentFromURL.OK.Click()
    

    比特很重要。如果我使用 64 位 Python,32 位 uTorrent 会崩溃。

    【讨论】:

    • 我不知道发生了什么,但即使我以前的代码似乎也不起作用。现在,当我运行代码时,只有 utorrent 打开,似乎什么也没发生。当我检查终端时,会出现回溯。回溯似乎与超时有关。即使我将超时设置为 10 或 15,也会出现同样的错误。如果您能给我完整的代码,我将不胜感激。我会看看它并了解出了什么问题。
    • 显然menu_item.edit1.TypeKeys("url to enter") 不正确。对不起,我没有时间为你写完整的剧本。如果没有追溯,我无法解释出了什么问题。如果您想添加更多详细信息,只需编辑您的问题。
    • 我编辑了它。我添加了回溯并放置了由 swapy 生成的确切代码,用于单击“从 URL 添加 Torrent”。当代码在 cmd 中运行时,utorrent 打开并且在 cmd 中给出回溯。没有其他事情发生。
    • 好的。请检查窗口打开时app.windows_() 返回的内容。一些应用程序会产生一个额外的进程,因此app.connect(title=u'\xb5Torrent4823DF041B09') 在这种情况下可能会有所帮助。
    • []
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    相关资源
    最近更新 更多