一些基础的操作SAP的Code
Open SAP
# Open SAP def open_sap(): try: Application(backend="win32").start(sap_path, timeout=60) log.info("Open SAP Successful") except: log.error("Open SAP Failed")
Enter AP1 and logon
# Enter AP1 then Logon def enter_sap_system(username, password, sid): try: # 登录AP1 (sid) log.info("Enter SAP system") app = Application(backend="win32").connect(path=sap_path) app.window(title='SAP Logon Pad 770').wait('exists ready', timeout=60) sap_window = app.window(title='SAP Logon Pad 770') sap_window.child_window(class_name="Edit").set_focus() sap_window.child_window(class_name="Edit").set_text(sid) sap_window.child_window(class_name="Edit").set_focus() send_keys("{ENTER}") time.sleep(2) sap_window.child_window(class_name="Edit").set_focus() sap_window.set_focus() send_keys("+{ENTER}") log.info("Enter sap system successful") session = "" time.sleep(2) # 连接SAP while True: try: pythoncom.CoInitialize() SapGuiAuto = win32com.client.GetObject("SAPGUI") application = SapGuiAuto.GetScriptingEngine if not type(application) == win32com.client.CDispatch: SapGuiAuto = None connection = application.Children(0) if not type(connection) == win32com.client.CDispatch: application = None SapGuiAuto = None session = connection.Children(0) if not type(session) == win32com.client.CDispatch: connection = None application = None SapGuiAuto = None break except: log.warning("Connect failed, please wait 3 seconds") time.sleep(3) # 登录 try: session.findById("wnd[0]/usr/txtRSYST-BNAME").text = username session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = password session.findById("wnd[0]/usr/txtRSYST-LANGU").text = "EN" session.findById("wnd[0]/tbar[0]/btn[0]").press() except: log.warning("Login failed") # 登录冲突,踢人 try: session.findById("wnd[1]/usr/radMULTI_LOGON_OPT1").select() session.findById("wnd[1]/tbar[0]/btn[0]").press() except: pass # 有时会弹出确认框,点击确认即可 try: time.sleep(3) session.findById("wnd[1]/tbar[0]/btn[0]").press() except: pass log.info("login successful, user:" + username) return session except: log.info("Enter SAP System failed")
退出SAP
def exit_after_n_second(n): time.sleep(n) # 关闭SAP __close_sap_window_by_class_name('SAP_FRONTEND_SESSION') __close_sap_window_by_title('SAP Logon Pad 770')
标签:findById,sap,log,Python,window,session,操作,SAP From: https://www.cnblogs.com/kykstyle/p/16813241.html