0

我正在使用 python 创建Microsoft SQL Server Management Studio 自动化工具。问题是我无法选择Child_tree (Northwind) 数据库 它正在选择Parent_tree (Databases)。我需要做更多,单击 child_tree(Northwind) 右键单击​​选项(例如任务-> 备份)。帮我做最好的自动化代码。提前致谢。


import pywinauto
import socket
import binascii
host = socket.gethostname()   #Getting system host name
n2 = int('0b111000001100001011100110111001101110111011011110111001001100100', 2) #password
n1 = int('0b111010101110011011001010111001001101110011000010110110101100101', 2) # username
n  = int('0b1110011011001010111001001110110011001010111001001101110011000010110110101100101', 2) #servername av
if (host == "systemhostXXX" or host == "systemhostyyy"): # checking the host name 
     try:
        pwa_app = pywinauto.application.Application()
        path = pwa_app.start_(r"C:/Program Files (x86)/Microsoft SQL Server/100/Tools/Binn/VSShell/Common7/IDE/Ssms.exe") #Opening the .exe file
print("Status: Application Launched successfully!!") except: print("Error: Applicatin Launching Error!!") try:

pwa_app.ConnecttoServer.ComboBox1.Select("Database Engine") #Selecting the combobox value pwa_app.ConnecttoServer.edit1.SetText(binascii.unhexlify('%x' % n)) pwa_app.ConnecttoServer.ComboBox3.Select("SQL Server Authentication") pwa_app.ConnecttoServer.edit2.SetText(binascii.unhexlify('%x' % n1)) # convert binary into string pwa_app.ConnecttoServer.edit3.SetText(binascii.unhexlify('%x' % n2)) print("Status: Log-in Process!!") pwa_app.ConnecttoServer.Connect.Click() except: print("Error: Log-In Failed!!Please Relaunch!") try: pwa_app.ConnecttoServer.Ok.Click() #Button click (OK) pwa_app.ConnecttoServer.Cancel.Click() print("Error: Restoration going-on!!") except: print("Status: Log-in Success!!") try: w_handle = pywinauto.findwindows.find_windows(title=u'Microsoft SQL Server Management Studio', class_name='wndclass_desked_gsk')[0] window = pwa_app.window_(handle=w_handle) ctrl = window['TreeView'] ctrl.GetItem([u'SQL Server 8.0.2039']).Click() ctrl.GetItem([u'SQL Server 8.0.2039', u'Databases', u'Northwind']).Click() #Selecting the database except: print("Database selection failed !!")

else: print 'Dear', host,'你没有被授权运行这个程序\n'

4

1 回答 1

0

正如我在评论中所理解的那样,您需要等到登录后主窗口打开。

window = pwa_app.Window_(title=u'Microsoft SQL Server Management Studio', class_name='wndclass_desked_gsk')
window.Wait('ready', timeout=20) # default timeout is 5 sec. if any
ctrl = window['TreeView']
ctrl.GetItem([u'SQL Server 8.0.2039']).Click() 
ctrl.GetItem([u'SQL Server 8.0.2039', u'Databases', u'Northwind']).Click() #Selecting the database

请检查它是如何工作的。

编辑

看来您'Microsoft SQL Server Management Studio'使用 SWAPY 生成了窗口代码。这意味着窗口已经打开。

但是在自动化工作流程Log-in中操作相当长(我相信可能需要长达 10 秒)。所以当你点击“连接”按钮时,'Microsoft SQL Server Management Studio'还没有打开。您可能会看到一些进度窗口,甚至几秒钟内什么也没有。

当窗口出现在屏幕上时,函数find_windows不会等待。它只是在那一刻找到了窗口。所以当你执行 line

window = pwa_app.Window_(title=u'Microsoft SQL Server Management Studio', class_name='wndclass_desked_gsk')

WindowSpecification对象被创建(window变量)。ctrl = window['TreeView']也是 WindowSpecification 对象。它们只是描述,与真实的窗口/控件无关。但是下面的语句

ctrl.GetItem([u'SQL Server 8.0.2039']).Click()

相当于

ctrl.WrapperObject().GetItem([u'SQL Server 8.0.2039']).Click() 

或者

ctrl.Wait('visible').GetItem([u'SQL Server 8.0.2039']).Click()

pywinautoWrapperObject()使用 Python 的强大功能隐藏调用。所以它是自动调用的。在这种情况下,默认超时为 5 秒。对于登录等长时间操作可能不够用。这就是为什么我建议Wait('ready', timeout=20)明确调用。'ready'表示'exists visible enabled'使用逻辑AND

于 2015-08-12T08:08:21.277 回答