***编辑
找到了更好/更简单的方法。此外,我将调用方法包装在 try 语句中,以便您可以将其与其他代码一起使用,并且在按钮可用之前它不会尝试单击保存/保存。
点击保存按钮:
import uiautomation as uia
SaveBtn = uia.SplitButtonControl(SearchDepth=4, Name = 'Save')
#wrap up the Invoke statement to try until the box shows up
while True:
try:
SaveBtn.GetInvokePattern().Invoke()
except LookupError:
time.sleep(0.5)
else:
break
单击下拉菜单以单击另存为并保存到位置:
import uiautomation as uia
SaveLocation = "C:\\Foobar\\FoobarDrive\\FobarMagic\\""
savestr = "Foobar.xlsx
#Click the Saveas Button on Internet Explorer Window
#clicking the down arrow next to save
SaveBtn = uia.SplitButtonControl(SearchDepth=5, Name = '6')
#wrap up the Invoke statement to try until the box shows up
while True:
try:
SaveBtn.GetInvokePattern().Invoke()
except LookupError:
time.sleep(0.5)
else:
break
#Find/map saveas button and click the saveas option
SaveasBtn = uia.MenuItemControl(SearchDepth=2, Name = 'Save as')
SaveasBtn.GetInvokePattern().Invoke()
#Find Save as dialog and click on address bar
Savedlg = uia.WindowControl(SearchDepth=1, Name = 'Save As')
#Name File
FileName = Savedlg.EditControl(SearchDepth=7,Name='File name:')
FileName.SendKeys(savestr)
#Click on the Previous locations button to activate the Addressbar
PrevLocBtn = Savedlg.ButtonControl(SearchDepth=7,Name = 'Previous Locations')
PrevLocBtn.GetInvokePattern().Invoke()
#Find the Address bar and clear it out
Addressbar = Savedlg.EditControl(SearchDepth=8, Name = 'Address')
Addressbar.SendKeys('{Back}')
#Define Save Location and Send the string to the addressbar
Addressbar.SendKeys(SaveLocation)
#Click go to directory button
Navigatebtn = Savedlg.ButtonControl(SearchDepth=6, Name= 'Go to ' + '"' + SaveLocation + '"')
while True:
try:
Navigatebtn.GetInvokePattern().Invoke()
except LookupError:
time.sleep(0.5)
else:
break
#Click Saveas Dlg Save Button
SaveBtn = Savedlg.ButtonControl(SearchDepth=2, Name="Save")
SaveBtn.GetInvokePattern().Invoke()
#Overwrite the old file if it exists
confirm = Savedlg.WindowControl(SearchDepth=2, Name = 'Confirm Save As')
confirmbtn = confirm.ButtonControl(SearchDepth=2, Name = 'Yes')
while True:
try:
confirmbtn.GetInvokePattern().Invoke()
except:
break
如何打开文件
#Click the Save Button on Internet Explorer Window
#clicking the save button itself.
OpenBtn = uia.SplitButtonControl(SearchDepth=4, Name = 'Open')
#wrap up the click statement to try until the box shows up
while True:
try:
OpenBtn.GetInvokePattern().Invoke()
except LookupError:
time.sleep(0.5)
else:
break