0

python を使用してMicrosoft SQL Server Management Studio 自動化ツールを作成しています。問題は、Child_tree (Northwind) データベースを選択できないことです。Parent_tree (データベース) を選択しています。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,'You are not Authorized to Run this program\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ウィンドウが画面に表示されている間、関数は待機しません。その瞬間にウィンドウを見つけるだけです。したがって、行を実行すると

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()

pywinauto はWrapperObject()、Python の力を使用して呼び出しを非表示にします。したがって、自動的に呼び出されます。この場合、デフォルトのタイムアウトは 5 秒です。ログインなど長時間の操作には物足りないかもしれません。Wait('ready', timeout=20)そのため、明示的に呼び出すことをお勧めします。logical を使用することを'ready'意味します。'exists visible enabled'AND

于 2015-08-12T08:08:21.277 に答える