print(‘count’, application.Children.Count)
#20210722新增
while not application.Children.Count:
time.sleep(0.5)
win32gui.SendMessage(dlg, win32con.WM_LBUTTONDOWN, 0)
win32gui.SendMessage(dlg, win32con.WM_LBUTTONUP, 0)
print('count 0 sleep 0.5')
SapGuiAuto = win32com.client.GetObject("SAPGUI")
application = SapGuiAuto.GetScriptingEngine
如果不能子进程数量为0,就延时0.5秒再点击一次登录按钮
def sap_login():
'''
登录SAP, 输入T-code, 返回获取的session
:return: session
'''
kill_sap()
sap_app = sap_path # 您的saplogon程序本地完整路径
subprocess.Popen(sap_app)
time.sleep(int(wait_time))
flt = 0
while flt == 0:
try:
hwnd = win32gui.FindWindow(None, sap_version)
flt = win32gui.FindWindowEx(hwnd, None, "Edit", None)
print('hwnd', hwnd)
print('flt', flt)
time.sleep(3)
print('---------------->')
except:
print('except============>')
time.sleep(0.5)
win32gui.SendMessage(flt, win32con.WM_SETTEXT, None, search_name)
win32gui.SendMessage(flt, win32con.WM_KEYDOWN, win32con.VK_RIGHT, 0)
win32gui.SendMessage(flt, win32con.WM_KEYUP, win32con.VK_RIGHT, 0)
time.sleep(2)
# 登录GUI界面
time.sleep(0.3)
dlg = win32gui.FindWindowEx(hwnd, None, "Button", None)
win32gui.SendMessage(dlg, win32con.WM_LBUTTONDOWN, 0)
win32gui.SendMessage(dlg, win32con.WM_LBUTTONUP, 0)
time.sleep(2)
SapGuiAuto = win32com.client.GetObject("SAPGUI")
print(SapGuiAuto)
print(type(SapGuiAuto))
if not type(SapGuiAuto) == win32com.client.CDispatch:
return
application = SapGuiAuto.GetScriptingEngine
print('application', application)
print('count', application.Children.Count)
#20210722新增
while not application.Children.Count:
time.sleep(0.5)
win32gui.SendMessage(dlg, win32con.WM_LBUTTONDOWN, 0)
win32gui.SendMessage(dlg, win32con.WM_LBUTTONUP, 0)
print('count 0 sleep 0.5')
SapGuiAuto = win32com.client.GetObject("SAPGUI")
application = SapGuiAuto.GetScriptingEngine
if not type(application) == win32com.client.CDispatch:
SapGuiAuto = None
return
connection = application.Children(0)
if not type(connection) == win32com.client.CDispatch:
application = None
SapGuiAuto = None
return
time.sleep(2)
flag = 0
while flag == 0:
try:
session = connection.Children(0)
flag = 1
except:
time.sleep(0.5)
# print('type session', type(session))
if not type(session) == win32com.client.CDispatch:
connection = None
application = None
SapGuiAuto = None
return
session.findById("wnd[0]").maximize()
session.findById("wnd[0]/usr/txtRSYST-MANDT").text = "807"
session.findById("wnd[0]/usr/txtRSYST-BNAME").text = username
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = password
session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus()
session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 8
session.findById("wnd[0]").sendVKey(0)
# 出现多用户登录
# License Information for Multiple Logon
multi_logon_text = session.findById("wnd[1]").text
if 'License Information' in multi_logon_text:
session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").select()
session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").setFocus()
session.findById("wnd[1]/tbar[0]/btn[0]").press()
# 出现版权点击确定,没有版权提示直接进行下一步
try:
x = session.findById("wnd[1]").text
# print(x)
if 'Copyright' in x:
session.findById("wnd[1]/tbar[0]/btn[0]").press()
except:
pass
# 出现二级密码登录,不出现跳过
try:
session.findById("wnd[1]/usr/txtGS_OUT-ID").text = username2
session.findById("wnd[1]/usr/pwdGS_OUT-PW").text = password2
session.findById("wnd[1]/usr/pwdGS_OUT-PW").setFocus()
session.findById("wnd[1]/usr/pwdGS_OUT-PW").caretPosition = 10
session.findById("wnd[1]/usr/btnLOGIN").press()
except:
print('no second user')
# 输入T-code
session.findById("wnd[0]/tbar[0]/okcd").text = "/NME23N"
session.findById("wnd[0]").sendVKey(0)
print('session type', type(session))
# with open('se', 'w') as f:
# f.write(session)
# print('write session')
return session