问题描述
假设我正在使用VLC Player,并且我想计算使用python启动所需的时间,有什么办法吗?
我尝试过winium自动化,但是元素加载太慢。我想获得应用程序的启动时间,并在4-5秒内启动,而winium通常花费更多的时间来查找元素的位置。
解决方法
如果我理解正确,那么您需要查找从执行该应用程序到窗口出现之间的时间。您可以使用pygetwindow库来实现。这是代码:
import pygetwindow as pw
import time
# here's code that executing the program,but i just put comment
start = time.time()
while 1: # waiting until window appears
# getWindowsWithTitle method returns empty list if it didn't found any windows with this name
if pw.getWindowsWithTitle('VLC'): #if it founds the window,loop will break
break
stop = time.time()
result = stop - start # result is time in seconds (type float)
编辑
您还可以通过检查窗口数量是否发生变化来检测应用程序的启动。
import pygetwindow as pw
import time
starting_amount = len(pw.getAllWindows()) # amount of windows
# here's code that executing the program,but i just put comment
start = time.time()
while 1: # waiting until window appears
if len(pw.getAllWindows()) > starting_amount: #if amount of windows is bigger than it was before we executed the program
break
stop = time.time()
result = stop - start # result is time in seconds (type float)
这应该适用于LD Player