|
# -*- coding: utf-8 -*- |
|
|
|
def app_start1(app1,path1,t1):#アプリの起動および接続 |
|
import time |
|
try: |
|
app1.Connect(path = path1) |
|
except: |
|
app1.Start(cmd_line = path1) |
|
time.sleep(t1) |
|
app1.Connect(path = path1) |
|
|
|
def button_click(app1,dialogtitle1,buttonname1):#指定したボタンをクリック |
|
import time |
|
dialog1 = app1[dialogtitle1]#指定したタイトルのダイアログを定義 |
|
dialog1_button1 = dialog1[buttonname1]#指定したタイトルのボタンを定義 |
|
if dialog1_button1.Exists():#指定したタイトルのボタンがあればクリック |
|
dialog1_button1.Click() |
|
time.sleep(0.5) |
|
for i in range(0, 5): |
|
if dialog1_button1.Exists(): |
|
dialog1_button1.Click() #上手く押せなかったら再度クリック |
|
time.sleep(0.5) |
|
else: |
|
break |
|
|
|
def callback(hwnd,IEServers): |
|
#「Internet Explorer_Server」クラスの子ウインドウハンドルを列挙して配列で返す |
|
#参考サイト Python : Automating Internet Explorer_Server |
|
#http://stackoverflow.com/questions/35778771/python-automating-internet-explorer-server |
|
import win32gui |
|
if win32gui.GetClassName(hwnd)==u"Internet Explorer_Server": |
|
IEServers.append(hwnd)#配列にハンドルを追加 |
|
print("IE server found:", hwnd) |
|
else: |
|
print("IE server not found:", hwnd) |
|
|
|
def gethtmldoc(hwnd1,num1): |
|
#HTMLドキュメントを取得する |
|
#参考サイト Python : Automating Internet Explorer_Server |
|
#http://stackoverflow.com/questions/35778771/python-automating-internet-explorer-server |
|
import win32gui |
|
import pythoncom |
|
import win32con |
|
import win32com |
|
IEServers = [] |
|
#「Internet Explorer_Server」クラスの子ウインドウハンドルを列挙して配列(IESetvers)に列挙 |
|
win32gui.EnumChildWindows(hwnd1, callback, IEServers) |
|
for serv in IEServers: |
|
#クラス名とタイトルを列挙 |
|
print(serv,",'",win32gui.GetClassName(serv),"','",win32gui.GetWindowText(serv),"'") |
|
if len(IEServers)>0: |
|
#HTMLドキュメントを取得するためのメッセージIDを取得 |
|
msg=win32gui.RegisterWindowMessage('WM_HTML_GETOBJECT') |
|
#メッセージ(msg)を送信 |
|
ret,result=win32gui.SendMessageTimeout(IEServers[num1],msg,0,0, \ |
|
win32con.SMTO_ABORTIFHUNG,1000) |
|
#メッセージの戻り値(result)からHTMLドキュメント(doc)を取得 |
|
ob=pythoncom.ObjectFromLresult(result,pythoncom.IID_IDispatch,0) |
|
doc = win32com.client.dynamic.Dispatch(ob) |
|
#HTMLドキュメントを返す |
|
return doc |
|
|
|
def htmlbutton_click(doc1,tagname1,buttonname2): |
|
#HTMLドキュメントの指定したタグ内で指定した名前のボタンを探して押す |
|
for i in range(0, 100): |
|
text1 = doc1.all.tags(tagname1)(i).Value |
|
print(u"doc.all.tags(tagname1)(",i,").Value=",text1) |
|
if text1 == buttonname2: |
|
num2 = i |
|
break |
|
elif text1 == "": |
|
break |
|
doc1.all.tags(tagname1)(num2).click()#ボタンを押す |
|
|
|
def app1start1():#アプリを起動→各ダイアログのボタンを押す |
|
import time |
|
from pywinauto.application import Application |
|
import win32gui |
|
|
|
#ソフト1を起動・接続 |
|
app = Application() |
|
path1 = u"C:\\Program Files (x86)\\software\\software1.exe" |
|
t1 = 2 |
|
app_start1(app,path1,t1) |
|
|
|
#ダイアログ1が開くので「はい」を押す |
|
dialogtitle1 = u"ソフト1" |
|
buttonname1 = u"はい(&Y)" |
|
button_click(app,dialogtitle1,buttonname1)#「はい」をクリック |
|
time.sleep(2) |
|
|
|
#ソフト2が自動で起動するので接続 |
|
app2 = Application() |
|
path2 = u"C:\\Program Files (x86)\\software\\software2.exe" |
|
t1 = 2 |
|
app_start1(app2,path2,t1) |
|
|
|
#ウインドウハンドルを取得 |
|
hwnd=win32gui.FindWindow(u"classname2",u"ソフト2") #クラス名(左記は適当)は別途事前に調べる |
|
print(hwnd) |
|
|
|
#「Internet Explorer_Server」クラスの子ウインドウのHTMLドキュメントを取得 |
|
num1 = 0 #1つ目の「Internet Explorer_Server」クラスの子ウインドウ |
|
doc = gethtmldoc(hwnd,num1)#HTMLドキュメントを取得 |
|
print(u"doc.URL=",doc.URL)#URLを確認 |
|
print(u"doc.all(0).innerHTML=",doc.all(0).innerHTML)#innerHTMLのテキストを確認 |
|
|
|
#innerHTMLよりログインボタンはINPUTタグ内にあるので探して押す |
|
tagname1 = u"INPUT" |
|
buttonname2 = u"ログイン" |
|
htmlbutton_click(doc,tagname1,buttonname2)#ログインボタンをクリック |
|
|
|
if __name__ == "__main__": |
|
|
|
app1start1() #アプリを開いてボタンを押す |