2017-04-17 82 views
0

我試圖使用pywinauto自動化窗口中的標準相機應用程序。pywinauto檢測是否存在動態控制

我可以成功的點擊通話等上存在的控制,但攝像頭的應用程序動態切換捕捉按鈕,當它從照片切換到視頻,所以我需要檢測它在哪一​​種模式。

from subprocess import Popen 
from pywinauto import Desktop 
from pywinauto.application import Application 

# ====== Take a Photo ======= 
# Need to get at the Camera app from desktop as there's multiple processes in the UI 
dlg = Desktop(backend="uia").Camera 
dlgWin = dlg.child_window(title="Camera", class_name="Windows.UI.Core.CoreWindow") 

# This fails: 
#existFlag = dlgWin.child_window(title="Take Photo", control_type="Button").Exists(timeout =2) 

# Take photo, this works if the control exists: 
buttonTakePhoto = dlgWin.child_window(title="Take Photo", control_type="Button") 
buttonTakePhoto.click() 

這裏的控制結構在照片模式時:

enter image description here

而且在視頻模式:

enter image description here

如何檢測窗口的子控件是否存在?我看到的所有例子都使用了一個我沒有的應用程序實例,因爲我將該窗口作爲桌面的子項。

回答

1

沒關係,我發誓,我想這一點,但它的工作原理:

buttonTakePhoto.exists()

+1

默認的超時時間爲5秒。所以timeout = 2可能不起作用,因爲它在2秒後仍然不存在。但我建議'buttonTakePhoto.wait('visible',timeout = 5)'而不是'.exists()'。 –