Pywinauto 计时等待 0.5 秒而不是立即

Moa*_*cir 6 python automation pywinauto python-3.x

我有以下 Pywinauto 代码,等待时间为 0.5 秒而不是立即。怎么立即拿到?

  • 要单击按钮并转到下一条消息:
from pywinauto import application, timings, mouse
import time

app = application.Application()
app.connect(title = 'Mensagem')
app = app.Mensagem
app2 = app.TBPanel2
buttonCoord = int((app2.rectangle().right - app2.rectangle().left)/2/2), int((app2.rectangle().bottom - app2.rectangle().top)/2)
buttonCoord = buttonCoord[0]*(2*2-1),buttonCoord[1]
timings.Timings.after_clickinput_wait = 0.001
timings.Timings.after_setcursorpos_wait = 0.001

starttime = time.perf_counter()
#while app.Edit1.texts()[0] != '':
for i in range(10):
    buttonCoord = int((app2.rectangle().right - app2.rectangle().left)/2/2), int((app2.rectangle().bottom - app2.rectangle().top)/2)
    buttonCoord = buttonCoord[0]*(2*2-1),buttonCoord[1]
    app2.click_input(button='left', coords=(buttonCoord))
    print('Entre cliques demorou ', str(time.perf_counter()-starttime), ' segundos')
    starttime = time.perf_counter()
Run Code Online (Sandbox Code Playgroud)

当我运行时,点击之间的间隔是 0.5 秒:

Entre cliques demorou  0.4614592999999999  segundos
Entre cliques demorou  0.44403170000000003  segundos
Entre cliques demorou  0.48520320000000017  segundos
Entre cliques demorou  0.4723533999999998  segundos
Entre cliques demorou  0.46825479999999997  segundos
Entre cliques demorou  0.4439942000000001  segundos
Entre cliques demorou  0.4874373999999997  segundos
Entre cliques demorou  0.47333040000000004  segundos
Entre cliques demorou  0.46036510000000064  segundos
Run Code Online (Sandbox Code Playgroud)

但是,当我疯狂地移动鼠标时,它会变得更快:

Entre cliques demorou  0.06659199999999998  segundos
Entre cliques demorou  0.1532768000000001  segundos
Entre cliques demorou  0.05349690000000007  segundos
Entre cliques demorou  0.049827499999999914  segundos
Entre cliques demorou  0.05078930000000015  segundos
Entre cliques demorou  0.04885250000000019  segundos
Entre cliques demorou  0.06023690000000004  segundos
Entre cliques demorou  0.048675000000000024  segundos
Entre cliques demorou  0.05394080000000012  segundos
Entre cliques demorou  0.05615450000000011  segundos
Run Code Online (Sandbox Code Playgroud)

我到底做错了什么?

编辑:对于更通用的方法和测试,调整代码,相同的结果

from pywinauto import application, timings
import time

app2 = application.Application()
app2.connect(title_re = '.*Notas*') #In portuguese, Notepad is translated to 'Bloco de Notas', so change to your system name
app2 = app2.window(title_re = '.*Notas*') #same here
app2 = app2.wrapper_object()
timings.Timings.after_clickinput_wait = 0.001
timings.Timings.after_setcursorpos_wait = 0.001
starttime = time.perf_counter()
for i in range(100):
    app2.click_input(button='left', coords=(100,100))
    print('Between clicks took ', str(time.perf_counter()-starttime), ' seconds')
    starttime = time.perf_counter()
Run Code Online (Sandbox Code Playgroud)

Vas*_*bov 1

You need to pre-search the element app2 and save it as wrapper object:

app2 = app.TBPanel2.wrapper_object()
# or
app2 = app.TBPanel2.wait('ready', timeout=10) # if you need non-default time to wait
Run Code Online (Sandbox Code Playgroud)

In your current code app2 = app.TBPanel2 is a WindowSpecification object which is just a criteria to search. So when you're calling .rectangle() or .click_input() the search procedure is called implicitly every time. This is described in the Getting Started Guide, Chapter "Window Specification".

Also I'd recommend to name the variable tb_panel2_spec or tb_panel2_wrapper instead of app2 to avoid confusion about its type.

P.S. Also the reliable cross-platform way to measure time in Python is import timeit; timeit.default_timer().

PS2: from pywinauto import mouse is not necessary because you're using method .click_input().


EDIT3:

The real problem is win32gui.GetDoubleClickTime() which returns 500 ms. Method .click_input() tries to eliminate double click this way. We can add optional parameter to disable this wait. You can workaround it by monkey patching win32gui. Add this code at the beginning:

import win32gui

def no_double_click_time():
    return 0

win32gui.GetDoubleClickTime = no_double_click_time
Run Code Online (Sandbox Code Playgroud)